Just case bash:
- if all 3 sides of the triangle are of equal length, then return
equilateral- then, check if the sum of the lengths of any 2 sides of the triangle is smaller than or equal to the length of the remaining triangle:
- if it is, then return
none- else, check if any 2 sides are of equal length:
- if there are, then return
isosceles- else, then return
scalene
code
class Solution {
public:
string triangleType(vector<int>& nums) {
if (nums[0] == nums[1] && nums[1] == nums[2]) {
return "equilateral";
}
for (int i = 0; i < 3; ++i) {
if (nums[i] + nums[(i+1)%3] <= nums[(i+2)%3]) {
return "none";
}
}
if (nums[0] == nums[1] || nums[0] == nums[2] || nums[1] == nums[2]) {
return "isosceles";
} else {
return "scalene";
}
}
};
complexity
- Time:
O(1)- Space:
O(1)
time taken
- 4 minutes and 27 seconds