mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2026-03-13 10:02:05 +08:00
添加 problem 976
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
package leetcode
|
||||
|
||||
func largestPerimeter(A []int) int {
|
||||
if len(A) < 3 {
|
||||
return 0
|
||||
}
|
||||
quickSort__(A, 0, len(A)-1)
|
||||
|
||||
for i := len(A) - 1; i >= 2; i-- {
|
||||
if (A[i]+A[i-1] > A[i-2]) && (A[i]+A[i-2] > A[i-1]) && (A[i-2]+A[i-1] > A[i]) {
|
||||
return A[i] + A[i-1] + A[i-2]
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question976 struct {
|
||||
para976
|
||||
ans976
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para976 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans976 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem976(t *testing.T) {
|
||||
|
||||
qs := []question976{
|
||||
|
||||
question976{
|
||||
para976{[]int{1, 2}},
|
||||
ans976{0},
|
||||
},
|
||||
question976{
|
||||
para976{[]int{1, 2, 3}},
|
||||
ans976{0},
|
||||
},
|
||||
|
||||
question976{
|
||||
para976{[]int{}},
|
||||
ans976{0},
|
||||
},
|
||||
|
||||
question976{
|
||||
para976{[]int{2, 1, 2}},
|
||||
ans976{5},
|
||||
},
|
||||
|
||||
question976{
|
||||
para976{[]int{1, 1, 2}},
|
||||
ans976{0},
|
||||
},
|
||||
|
||||
question976{
|
||||
para976{[]int{3, 2, 3, 4}},
|
||||
ans976{10},
|
||||
},
|
||||
|
||||
question976{
|
||||
para976{[]int{3, 6, 2, 3}},
|
||||
ans976{8},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 976------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans976, q.para976
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, largestPerimeter(p.one))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
47
Algorithms/976. Largest Perimeter Triangle/README.md
Normal file
47
Algorithms/976. Largest Perimeter Triangle/README.md
Normal file
@@ -0,0 +1,47 @@
|
||||
# [976. Largest Perimeter Triangle](https://leetcode.com/problems/largest-perimeter-triangle/)
|
||||
|
||||
## 题目
|
||||
|
||||
Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero area, formed from 3 of these lengths.
|
||||
|
||||
If it is impossible to form any triangle of non-zero area, return 0.
|
||||
|
||||
|
||||
Example 1:
|
||||
|
||||
```c
|
||||
Input: [2,1,2]
|
||||
Output: 5
|
||||
```
|
||||
|
||||
Example 2:
|
||||
|
||||
```c
|
||||
Input: [1,2,1]
|
||||
Output: 0
|
||||
```
|
||||
|
||||
Example 3:
|
||||
|
||||
```c
|
||||
Input: [3,2,3,4]
|
||||
Output: 10
|
||||
```
|
||||
|
||||
Example 4:
|
||||
|
||||
```c
|
||||
Input: [3,6,2,3]
|
||||
Output: 8
|
||||
```
|
||||
|
||||
Note:
|
||||
|
||||
- 3 <= A.length <= 10000
|
||||
- 1 <= A[i] <= 10^6
|
||||
|
||||
## 题目大意
|
||||
|
||||
找到可以组成三角形三条边的长度,要求输出三条边之和最长的,即三角形周长最长。
|
||||
|
||||
这道题也是排序题,先讲所有的长度进行排序,从大边开始往前找,找到第一个任意两边之和大于第三边(满足能构成三角形的条件)的下标,然后输出这 3 条边之和即可,如果没有找到输出 0 。
|
||||
Reference in New Issue
Block a user