You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: solution/3000-3099/3024.Type of Triangle II/README_EN.md
+79-4Lines changed: 79 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -46,24 +46,99 @@ As all the sides are of different lengths, it will form a scalene triangle.
46
46
47
47
## Solutions
48
48
49
-
### Solution 1
49
+
### Solution 1: Sorting + Case Discussion
50
+
51
+
First, we sort the array, and then we can classify and discuss according to the definition of a triangle.
52
+
53
+
- If the sum of the smallest two numbers is less than or equal to the largest number, then it cannot form a triangle, return "none".
54
+
- If the smallest number is equal to the largest number, then it is an equilateral triangle, return "equilateral".
55
+
- If the smallest number is equal to the middle number or the middle number is equal to the largest number, then it is an isosceles triangle, return "isosceles".
56
+
- Otherwise, return "scalene".
57
+
58
+
The time complexity is $O(1)$, and the space complexity is $O(1)$.
50
59
51
60
<!-- tabs:start -->
52
61
53
62
```python
54
-
63
+
classSolution:
64
+
deftriangleType(self, nums: List[int]) -> str:
65
+
nums.sort()
66
+
if nums[0] + nums[1] <= nums[2]:
67
+
return"none"
68
+
if nums[0] == nums[2]:
69
+
return"equilateral"
70
+
if nums[0] == nums[1] or nums[1] == nums[2]:
71
+
return"isosceles"
72
+
return"scalene"
55
73
```
56
74
57
75
```java
58
-
76
+
classSolution {
77
+
publicStringtriangleType(int[] nums) {
78
+
Arrays.sort(nums);
79
+
if (nums[0] + nums[1] <= nums[2]) {
80
+
return"none";
81
+
}
82
+
if (nums[0] == nums[2]) {
83
+
return"equilateral";
84
+
}
85
+
if (nums[0] == nums[1] || nums[1] == nums[2]) {
86
+
return"isosceles";
87
+
}
88
+
return"scalene";
89
+
}
90
+
}
59
91
```
60
92
61
93
```cpp
62
-
94
+
classSolution {
95
+
public:
96
+
string triangleType(vector<int>& nums) {
97
+
sort(nums.begin(), nums.end());
98
+
if (nums[0] + nums[1] <= nums[2]) {
99
+
return "none";
100
+
}
101
+
if (nums[0] == nums[2]) {
102
+
return "equilateral";
103
+
}
104
+
if (nums[0] == nums[1] || nums[1] == nums[2]) {
105
+
return "isosceles";
106
+
}
107
+
return "scalene";
108
+
}
109
+
};
63
110
```
64
111
65
112
```go
113
+
func triangleType(nums []int) string {
114
+
sort.Ints(nums)
115
+
if nums[0]+nums[1] <= nums[2] {
116
+
return "none"
117
+
}
118
+
if nums[0] == nums[2] {
119
+
return "equilateral"
120
+
}
121
+
if nums[0] == nums[1] || nums[1] == nums[2] {
122
+
return "isosceles"
123
+
}
124
+
return "scalene"
125
+
}
126
+
```
66
127
128
+
```ts
129
+
function triangleType(nums:number[]):string {
130
+
nums.sort((a, b) =>a-b);
131
+
if (nums[0] +nums[1] <=nums[2]) {
132
+
return'none';
133
+
}
134
+
if (nums[0] ===nums[2]) {
135
+
return'equilateral';
136
+
}
137
+
if (nums[0] ===nums[1] ||nums[1] ===nums[2]) {
138
+
return'isosceles';
139
+
}
140
+
return'scalene';
141
+
}
67
142
```
68
143
69
144
<!-- tabs:end -->
Collapse file: solution/3000-3099/3024.Type of Triangle II/Solution.cpp
0 commit comments