[LeetCode] Type of Triangle II

3024. Type of Triangle II

You are given a 0-indexed integer array nums of size 3 which can form the sides of a triangle.

  • A triangle is called equilateral if it has all sides of equal length.
  • A triangle is called isosceles if it has exactly two sides of equal length.
  • A triangle is called scalene if all its sides are of different lengths.

Return a string representing the type of triangle that can be formed or "none" if it cannot form a triangle.

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
string triangleType(vector<int>& A) {
sort(begin(A), end(A));
if(A[0] == A[2]) return "equilateral";
if(A[0] + A[1] <= A[2]) return "none";
if(A[0] == A[1]) return "isosceles";
if(A[1] == A[2]) return "isosceles";
return "scalene";
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/02/04/PS/LeetCode/type-of-triangle-ii/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.