File tree 6 files changed +118
-53
lines changed
solution/0900-0999/0976.Largest Perimeter Triangle
6 files changed +118
-53
lines changed Original file line number Diff line number Diff line change 40
40
41
41
<!-- 这里可写通用的实现逻辑 -->
42
42
43
+ ** 方法一:排序 + 贪心**
44
+
43
45
> 三角形由三条边组成,且满足 <var >C</var > >= <var >B</var > && <var >C</var > >= <var >A</var > && <var >C</var > < <var >A</var > + <var >B</var >
44
46
45
47
贪心策略,尽可能使用长边来组成三角形。
56
58
<!-- 这里可写当前语言的特殊实现逻辑 -->
57
59
58
60
``` python
59
-
61
+ class Solution :
62
+ def largestPerimeter (self , nums : List[int ]) -> int :
63
+ nums.sort()
64
+ for i in range (len (nums) - 1 , 1 , - 1 ):
65
+ if (c := nums[i - 1 ] + nums[i - 2 ]) > nums[i]:
66
+ return c + nums[i]
67
+ return 0
60
68
```
61
69
62
70
### ** Java**
63
71
64
72
<!-- 这里可写当前语言的特殊实现逻辑 -->
65
73
66
74
``` java
67
-
75
+ class Solution {
76
+ public int largestPerimeter (int [] nums ) {
77
+ Arrays . sort(nums);
78
+ for (int i = nums. length - 1 ; i >= 2 ; -- i) {
79
+ int c = nums[i - 1 ] + nums[i - 2 ];
80
+ if (c > nums[i]) {
81
+ return c + nums[i];
82
+ }
83
+ }
84
+ return 0 ;
85
+ }
86
+ }
68
87
```
69
88
70
89
### ** C++**
71
90
72
91
``` cpp
73
92
class Solution {
74
93
public:
75
- int largestPerimeter(vector<int >& A) {
76
- priority_queue<int > q(A.begin(), A.end()) ; // 大顶堆
77
-
78
- int a, b, c ;
79
- b = q.top() ;
80
- q.pop() ;
81
- c = q.top() ;
82
- q.pop() ;
83
- while ( !q.empty() )
94
+ int largestPerimeter(vector<int >& nums) {
95
+ sort(nums.begin(), nums.end());
96
+ for (int i = nums.size() - 1; i >= 2; --i)
84
97
{
85
- a = b ;
86
- b = c ;
87
- c = q.top() ;
88
- q.pop() ;
89
- if ( b + c > a )
90
- return a + b + c ;
98
+ int c = nums[ i - 1] + nums[ i - 2] ;
99
+ if (c > nums[ i] ) return c + nums[ i] ;
91
100
}
92
- return 0 ;
101
+ return 0;
93
102
}
94
103
};
95
104
```
96
105
106
+ ### **Go**
107
+
108
+ ```go
109
+ func largestPerimeter(nums []int) int {
110
+ sort.Ints(nums)
111
+ for i := len(nums) - 1; i >= 2; i-- {
112
+ c := nums[i-1] + nums[i-2]
113
+ if c > nums[i] {
114
+ return c + nums[i]
115
+ }
116
+ }
117
+ return 0
118
+ }
119
+ ```
120
+
97
121
### ** TypeScript**
98
122
99
123
``` ts
Original file line number Diff line number Diff line change 36
36
### ** Python3**
37
37
38
38
``` python
39
-
39
+ class Solution :
40
+ def largestPerimeter (self , nums : List[int ]) -> int :
41
+ nums.sort()
42
+ for i in range (len (nums) - 1 , 1 , - 1 ):
43
+ if (c := nums[i - 1 ] + nums[i - 2 ]) > nums[i]:
44
+ return c + nums[i]
45
+ return 0
40
46
```
41
47
42
48
### ** Java**
43
49
44
50
``` java
45
-
51
+ class Solution {
52
+ public int largestPerimeter (int [] nums ) {
53
+ Arrays . sort(nums);
54
+ for (int i = nums. length - 1 ; i >= 2 ; -- i) {
55
+ int c = nums[i - 1 ] + nums[i - 2 ];
56
+ if (c > nums[i]) {
57
+ return c + nums[i];
58
+ }
59
+ }
60
+ return 0 ;
61
+ }
62
+ }
46
63
```
47
64
48
65
### ** C++**
49
66
50
67
``` cpp
51
68
class Solution {
52
69
public:
53
- int largestPerimeter(vector<int >& A) {
54
- priority_queue<int > q(A.begin(), A.end()) ; // 大顶堆
55
-
56
- int a, b, c ;
57
- b = q.top() ;
58
- q.pop() ;
59
- c = q.top() ;
60
- q.pop() ;
61
- while ( !q.empty() )
70
+ int largestPerimeter(vector<int >& nums) {
71
+ sort(nums.begin(), nums.end());
72
+ for (int i = nums.size() - 1; i >= 2; --i)
62
73
{
63
- a = b ;
64
- b = c ;
65
- c = q.top() ;
66
- q.pop() ;
67
- if ( b + c > a )
68
- return a + b + c ;
74
+ int c = nums[ i - 1] + nums[ i - 2] ;
75
+ if (c > nums[ i] ) return c + nums[ i] ;
69
76
}
70
- return 0 ;
77
+ return 0;
71
78
}
72
79
};
73
80
```
74
81
82
+ ### **Go**
83
+
84
+ ```go
85
+ func largestPerimeter(nums []int) int {
86
+ sort.Ints(nums)
87
+ for i := len(nums) - 1; i >= 2; i-- {
88
+ c := nums[i-1] + nums[i-2]
89
+ if c > nums[i] {
90
+ return c + nums[i]
91
+ }
92
+ }
93
+ return 0
94
+ }
95
+ ```
96
+
75
97
### ** TypeScript**
76
98
77
99
``` ts
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public:
3
- int largestPerimeter (vector<int >& A) {
4
- priority_queue<int > q (A.begin (), A.end ()) ; // 大顶堆
5
-
6
- int a, b, c ;
7
- b = q.top () ;
8
- q.pop () ;
9
- c = q.top () ;
10
- q.pop () ;
11
- while ( !q.empty () )
3
+ int largestPerimeter (vector<int >& nums) {
4
+ sort (nums.begin (), nums.end ());
5
+ for (int i = nums.size () - 1 ; i >= 2 ; --i)
12
6
{
13
- a = b ;
14
- b = c ;
15
- c = q.top () ;
16
- q.pop () ;
17
- if ( b + c > a )
18
- return a + b + c ;
7
+ int c = nums[i - 1 ] + nums[i - 2 ];
8
+ if (c > nums[i]) return c + nums[i];
19
9
}
20
- return 0 ;
10
+ return 0 ;
21
11
}
22
- };
12
+ };
Original file line number Diff line number Diff line change
1
+ func largestPerimeter (nums []int ) int {
2
+ sort .Ints (nums )
3
+ for i := len (nums ) - 1 ; i >= 2 ; i -- {
4
+ c := nums [i - 1 ] + nums [i - 2 ]
5
+ if c > nums [i ] {
6
+ return c + nums [i ]
7
+ }
8
+ }
9
+ return 0
10
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int largestPerimeter (int [] nums ) {
3
+ Arrays .sort (nums );
4
+ for (int i = nums .length - 1 ; i >= 2 ; --i ) {
5
+ int c = nums [i - 1 ] + nums [i - 2 ];
6
+ if (c > nums [i ]) {
7
+ return c + nums [i ];
8
+ }
9
+ }
10
+ return 0 ;
11
+ }
12
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def largestPerimeter (self , nums : List [int ]) -> int :
3
+ nums .sort ()
4
+ for i in range (len (nums ) - 1 , 1 , - 1 ):
5
+ if (c := nums [i - 1 ] + nums [i - 2 ]) > nums [i ]:
6
+ return c + nums [i ]
7
+ return 0
You can’t perform that action at this time.
0 commit comments