Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.3024 (#2310)
Browse files Browse the repository at this point in the history
No.3024.Type of Triangle II
  • Loading branch information
yanglbme authored Feb 4, 2024
1 parent 3d68c8a commit 745dd62
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 8 deletions.
83 changes: 79 additions & 4 deletions solution/3000-3099/3024.Type of Triangle II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,99 @@ nums[1] + nums[2] = 4 + 5 = 9 ,大于 nums[0] = 3 。

## 解法

### 方法一
### 方法一:排序 + 分类讨论

我们先对数组进行排序,然后根据三角形的定义进行分类讨论即可。

- 如果最小的两个数之和小于等于最大的数,那么无法构成三角形,返回 "none"。
- 如果最小的数等于最大的数,那么是等边三角形,返回 "equilateral"。
- 如果最小的数等于中间的数或者中间的数等于最大的数,那么是等腰三角形,返回 "isosceles"。
- 否则,返回 "scalene"。

时间复杂度 $O(1)$,空间复杂度 $O(1)$。

<!-- tabs:start -->

```python

class Solution:
def triangleType(self, nums: List[int]) -> str:
nums.sort()
if nums[0] + nums[1] <= nums[2]:
return "none"
if nums[0] == nums[2]:
return "equilateral"
if nums[0] == nums[1] or nums[1] == nums[2]:
return "isosceles"
return "scalene"
```

```java

class Solution {
public String triangleType(int[] nums) {
Arrays.sort(nums);
if (nums[0] + nums[1] <= nums[2]) {
return "none";
}
if (nums[0] == nums[2]) {
return "equilateral";
}
if (nums[0] == nums[1] || nums[1] == nums[2]) {
return "isosceles";
}
return "scalene";
}
}
```

```cpp

class Solution {
public:
string triangleType(vector<int>& nums) {
sort(nums.begin(), nums.end());
if (nums[0] + nums[1] <= nums[2]) {
return "none";
}
if (nums[0] == nums[2]) {
return "equilateral";
}
if (nums[0] == nums[1] || nums[1] == nums[2]) {
return "isosceles";
}
return "scalene";
}
};
```
```go
func triangleType(nums []int) string {
sort.Ints(nums)
if nums[0]+nums[1] <= nums[2] {
return "none"
}
if nums[0] == nums[2] {
return "equilateral"
}
if nums[0] == nums[1] || nums[1] == nums[2] {
return "isosceles"
}
return "scalene"
}
```

```ts
function triangleType(nums: number[]): string {
nums.sort((a, b) => a - b);
if (nums[0] + nums[1] <= nums[2]) {
return 'none';
}
if (nums[0] === nums[2]) {
return 'equilateral';
}
if (nums[0] === nums[1] || nums[1] === nums[2]) {
return 'isosceles';
}
return 'scalene';
}
```

<!-- tabs:end -->
Expand Down
83 changes: 79 additions & 4 deletions solution/3000-3099/3024.Type of Triangle II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,99 @@ As all the sides are of different lengths, it will form a scalene triangle.

## Solutions

### Solution 1
### Solution 1: Sorting + Case Discussion

First, we sort the array, and then we can classify and discuss according to the definition of a triangle.

- 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".
- If the smallest number is equal to the largest number, then it is an equilateral triangle, return "equilateral".
- 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".
- Otherwise, return "scalene".

The time complexity is $O(1)$, and the space complexity is $O(1)$.

<!-- tabs:start -->

```python

class Solution:
def triangleType(self, nums: List[int]) -> str:
nums.sort()
if nums[0] + nums[1] <= nums[2]:
return "none"
if nums[0] == nums[2]:
return "equilateral"
if nums[0] == nums[1] or nums[1] == nums[2]:
return "isosceles"
return "scalene"
```

```java

class Solution {
public String triangleType(int[] nums) {
Arrays.sort(nums);
if (nums[0] + nums[1] <= nums[2]) {
return "none";
}
if (nums[0] == nums[2]) {
return "equilateral";
}
if (nums[0] == nums[1] || nums[1] == nums[2]) {
return "isosceles";
}
return "scalene";
}
}
```

```cpp

class Solution {
public:
string triangleType(vector<int>& nums) {
sort(nums.begin(), nums.end());
if (nums[0] + nums[1] <= nums[2]) {
return "none";
}
if (nums[0] == nums[2]) {
return "equilateral";
}
if (nums[0] == nums[1] || nums[1] == nums[2]) {
return "isosceles";
}
return "scalene";
}
};
```
```go
func triangleType(nums []int) string {
sort.Ints(nums)
if nums[0]+nums[1] <= nums[2] {
return "none"
}
if nums[0] == nums[2] {
return "equilateral"
}
if nums[0] == nums[1] || nums[1] == nums[2] {
return "isosceles"
}
return "scalene"
}
```

```ts
function triangleType(nums: number[]): string {
nums.sort((a, b) => a - b);
if (nums[0] + nums[1] <= nums[2]) {
return 'none';
}
if (nums[0] === nums[2]) {
return 'equilateral';
}
if (nums[0] === nums[1] || nums[1] === nums[2]) {
return 'isosceles';
}
return 'scalene';
}
```

<!-- tabs:end -->
Expand Down
16 changes: 16 additions & 0 deletions solution/3000-3099/3024.Type of Triangle II/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public:
string triangleType(vector<int>& nums) {
sort(nums.begin(), nums.end());
if (nums[0] + nums[1] <= nums[2]) {
return "none";
}
if (nums[0] == nums[2]) {
return "equilateral";
}
if (nums[0] == nums[1] || nums[1] == nums[2]) {
return "isosceles";
}
return "scalene";
}
};
13 changes: 13 additions & 0 deletions solution/3000-3099/3024.Type of Triangle II/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
func triangleType(nums []int) string {
sort.Ints(nums)
if nums[0]+nums[1] <= nums[2] {
return "none"
}
if nums[0] == nums[2] {
return "equilateral"
}
if nums[0] == nums[1] || nums[1] == nums[2] {
return "isosceles"
}
return "scalene"
}
15 changes: 15 additions & 0 deletions solution/3000-3099/3024.Type of Triangle II/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public String triangleType(int[] nums) {
Arrays.sort(nums);
if (nums[0] + nums[1] <= nums[2]) {
return "none";
}
if (nums[0] == nums[2]) {
return "equilateral";
}
if (nums[0] == nums[1] || nums[1] == nums[2]) {
return "isosceles";
}
return "scalene";
}
}
10 changes: 10 additions & 0 deletions solution/3000-3099/3024.Type of Triangle II/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution:
def triangleType(self, nums: List[int]) -> str:
nums.sort()
if nums[0] + nums[1] <= nums[2]:
return "none"
if nums[0] == nums[2]:
return "equilateral"
if nums[0] == nums[1] or nums[1] == nums[2]:
return "isosceles"
return "scalene"
13 changes: 13 additions & 0 deletions solution/3000-3099/3024.Type of Triangle II/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function triangleType(nums: number[]): string {
nums.sort((a, b) => a - b);
if (nums[0] + nums[1] <= nums[2]) {
return 'none';
}
if (nums[0] === nums[2]) {
return 'equilateral';
}
if (nums[0] === nums[1] || nums[1] === nums[2]) {
return 'isosceles';
}
return 'scalene';
}

0 comments on commit 745dd62

Please sign in to comment.