Skip to content

Commit 663b1cb

Browse files
authored
Merge pull request #1325 from 0xff-dev/976
Add solution and test-cases for problem 976
2 parents fe6be0e + 534ef64 commit 663b1cb

File tree

3 files changed

+32
-23
lines changed

3 files changed

+32
-23
lines changed

leetcode/901-1000/0976.Largest-Perimeter-Triangle/README.md

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
# [976.Largest Perimeter Triangle][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Given an integer array `nums`, return the largest perimeter of a triangle with a non-zero area, formed from three of these lengths. If it is impossible to form any triangle of a non-zero area, return `0`.
75

86
**Example 1:**
97

108
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
9+
Input: nums = [2,1,2]
10+
Output: 5
11+
Explanation: You can form a triangle with three side lengths: 1, 2, and 2.
1312
```
1413

15-
## 题意
16-
> ...
17-
18-
## 题解
14+
**Example 2:**
1915

20-
### 思路1
21-
> ...
22-
Largest Perimeter Triangle
23-
```go
2416
```
25-
17+
Input: nums = [1,2,1,10]
18+
Output: 0
19+
Explanation:
20+
You cannot use the side lengths 1, 1, and 2 to form a triangle.
21+
You cannot use the side lengths 1, 1, and 10 to form a triangle.
22+
You cannot use the side lengths 1, 2, and 10 to form a triangle.
23+
As we cannot use any three side lengths to form a triangle of non-zero area, we return 0.
24+
```
2625

2726
## 结语
2827

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(nums []int) int {
6+
sort.Ints(nums)
7+
// 1, 1, 2, 10
8+
l := len(nums)
9+
for i := l - 1; i >= 2; i-- {
10+
a, b, c := nums[i], nums[i-1], nums[i-2]
11+
if a+b > c && a+c > b && b+c > a {
12+
return a + b + c
13+
}
14+
}
15+
return 0
516
}

leetcode/901-1000/0976.Largest-Perimeter-Triangle/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{2, 1, 2}, 5},
17+
{"TestCase2", []int{1, 2, 1, 10}, 0},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
//压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
//使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)