Skip to content

Commit 9e51c1f

Browse files
committed
update: 添加问题“976.三角形的最大周长”的代码和题解 (#1150)
0976: AC.cpp+py+java+go+rust (#1149) cpp - AC,58.29%,26.60% rust - AC,-%,100.00% java - AC,96.37%,99.05% go - AC,47.06%,100.00% py - AC,34.41%,25.76% Signed-off-by: LetMeFly666 <[email protected]>
1 parent ceb6160 commit 9e51c1f

15 files changed

+293
-16
lines changed

#include <iostream>.cpp

Lines changed: 0 additions & 1 deletion
This file was deleted.

.commitmsg

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
cpp - AC,33.52%,100.00%
2-
py - AC,62.32%,81.16%
3-
py - AC,py-oneline,86.23%,45.65%
1+
0976: AC.cpp+py+java+go+rust (#1149)
2+
3+
cpp - AC,58.29%,26.60%
4+
rust - AC,-%,100.00%
5+
java - AC,96.37%,99.05%
6+
go - AC,47.06%,100.00%
7+
py - AC,34.41%,25.76%
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-09-28 13:21:46
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-09-28 13:22:32
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int largestPerimeter(vector<int>& nums) {
14+
sort(nums.begin(), nums.end());
15+
for (int i = nums.size() - 3; i >= 0; i--) {
16+
if (nums[i] + nums[i + 1] > nums[i + 2]) {
17+
return nums[i] + nums[i + 1] + nums[i + 2];
18+
}
19+
}
20+
return 0;
21+
}
22+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-09-28 13:21:46
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-09-28 13:29:34
6+
*/
7+
package main
8+
9+
import "sort"
10+
11+
func largestPerimeter(nums []int) int {
12+
sort.Ints(nums)
13+
for i := len(nums) - 3; i >= 0; i-- {
14+
if nums[i] + nums[i + 1] > nums[i + 2] {
15+
return nums[i] + nums[i + 1] + nums[i + 2]
16+
}
17+
}
18+
return 0
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-09-28 13:21:46
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-09-28 13:27:11
6+
*/
7+
import java.util.Arrays;
8+
9+
class Solution {
10+
public int largestPerimeter(int[] nums) {
11+
Arrays.sort(nums);
12+
for (int i = nums.length - 3; i >= 0; i--) {
13+
if (nums[i] + nums[i + 1] > nums[i + 2]) {
14+
return nums[i] + nums[i + 1] + nums[i + 2];
15+
}
16+
}
17+
return 0;
18+
}
19+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-09-28 13:21:46
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-09-28 13:30:31
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def largestPerimeter(self, nums: List[int]) -> int:
11+
nums.sort()
12+
for i in range(len(nums) - 3, -1, -1):
13+
if nums[i] + nums[i + 1] > nums[i + 2]:
14+
return nums[i] + nums[i + 1] + nums[i + 2]
15+
return 0
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-09-28 13:21:46
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-09-28 13:25:57
6+
*/
7+
impl Solution {
8+
pub fn largest_perimeter(mut nums: Vec<i32>) -> i32 {
9+
nums.sort();
10+
for i in (0..nums.len()-2).rev() {
11+
if nums[i] + nums[i + 1] > nums[i + 2] {
12+
return nums[i] + nums[i + 1] + nums[i + 2];
13+
}
14+
}
15+
0
16+
}
17+
}

Codes/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
* @LastEditTime: 2025-09-07 23:24:39
66
*/
77
pub struct Solution;
8-
include!("0611-valid-triangle-number_20250926.rs"); // 这个fileName是会被脚本替换掉的
8+
include!("0976-largest-perimeter-triangle.rs"); // 这个fileName是会被脚本替换掉的

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@
459459
|0961.在长度2N的数组中找出重复N次的元素|简单|<a href="https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/05/21/LeetCode%200961.%E5%9C%A8%E9%95%BF%E5%BA%A62N%E7%9A%84%E6%95%B0%E7%BB%84%E4%B8%AD%E6%89%BE%E5%87%BA%E9%87%8D%E5%A4%8DN%E6%AC%A1%E7%9A%84%E5%85%83%E7%B4%A0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/124897591" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-closest-palindrome/solution/letmefly-da-an-hou-xuan-by-letmefly-2-k2vn/" target="_blank">LeetCode题解</a>|
460460
|0966.元音拼写检查器|中等|<a href="https://leetcode.cn/problems/vowel-spellchecker/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/09/14/LeetCode%200966.%E5%85%83%E9%9F%B3%E6%8B%BC%E5%86%99%E6%A3%80%E6%9F%A5%E5%99%A8/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/151690858" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/vowel-spellchecker/solutions/3781983/letmefly-966yuan-yin-pin-xie-jian-cha-qi-16uh/" target="_blank">LeetCode题解</a>|
461461
|0970.强整数|中等|<a href="https://leetcode.cn/problems/powerful-integers/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/05/02/LeetCode%200970.%E5%BC%BA%E6%95%B4%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/130461751" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/powerful-integers/solutions/2253101/letmefly-970qiang-zheng-shu-by-tisfy-w22m/" target="_blank">LeetCode题解</a>|
462+
|0976.三角形的最大周长|简单|<a href="https://leetcode.cn/problems/largest-perimeter-triangle/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/09/28/LeetCode%200976.%E4%B8%89%E8%A7%92%E5%BD%A2%E7%9A%84%E6%9C%80%E5%A4%A7%E5%91%A8%E9%95%BF/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/152215094" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/largest-perimeter-triangle/solutions/3794128/letmefly-976san-jiao-xing-de-zui-da-zhou-atq1/" target="_blank">LeetCode题解</a>|
462463
|0982.按位与为零的三元组|困难|<a href="https://leetcode.cn/problems/triples-with-bitwise-and-equal-to-zero/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/03/04/LeetCode%200982.%E6%8C%89%E4%BD%8D%E4%B8%8E%E4%B8%BA%E9%9B%B6%E7%9A%84%E4%B8%89%E5%85%83%E7%BB%84/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/129334313" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/triples-with-bitwise-and-equal-to-zero/solutions/2146568/letmefly-982an-wei-yu-wei-ling-de-san-yu-071t/" target="_blank">LeetCode题解</a>|
463464
|0983.最低票价|中等|<a href="https://leetcode.cn/problems/minimum-cost-for-tickets/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/10/01/LeetCode%200983.%E6%9C%80%E4%BD%8E%E7%A5%A8%E4%BB%B7/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/142672522" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-cost-for-tickets/solutions/2936476/letmefly-983zui-di-piao-jie-ji-yi-hua-so-o7w3/" target="_blank">LeetCode题解</a>|
464465
|0987.二叉树的垂序遍历|困难|<a href="https://leetcode.cn/problems/vertical-order-traversal-of-a-binary-tree/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/02/13/LeetCode%200987.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E5%9E%82%E5%BA%8F%E9%81%8D%E5%8E%86/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/136106019" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/vertical-order-traversal-of-a-binary-tree/solutions/2639036/letmefly-987er-cha-shu-de-chui-xu-bian-l-l9yo/" target="_blank">LeetCode题解</a>|
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
---
2+
title: 976.三角形的最大周长:贪心(排序)
3+
date: 2025-09-28 13:31:12
4+
tags: [题解, LeetCode, 简单, 贪心, 数组, 数学, 排序]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】976.三角形的最大周长:贪心(排序)
9+
10+
力扣题目链接:[https://leetcode.cn/problems/largest-perimeter-triangle/](https://leetcode.cn/problems/largest-perimeter-triangle/)
11+
12+
<p>给定由一些正数(代表长度)组成的数组 <code>nums</code>&nbsp;,返回 <em>由其中三个长度组成的、<strong>面积不为零</strong>的三角形的最大周长</em>&nbsp;。如果不能形成任何面积不为零的三角形,返回&nbsp;<code>0</code>。</p>
13+
14+
<p>&nbsp;</p>
15+
16+
<ol>
17+
</ol>
18+
19+
<p><strong>示例 1:</strong></p>
20+
21+
<pre>
22+
<strong>输入:</strong>nums = [2,1,2]
23+
<strong>输出:</strong>5
24+
<strong>解释:</strong>你可以用三个边长组成一个三角形:1 2 2。
25+
</pre>
26+
27+
<p><strong>示例 2:</strong></p>
28+
29+
<pre>
30+
<strong>输入:</strong>nums = [1,2,1,10]
31+
<strong>输出:</strong>0
32+
<strong>解释:</strong>
33+
你不能用边长 1,1,2 来组成三角形。
34+
不能用边长 1,1,10 来构成三角形。
35+
不能用边长 1、2 和 10 来构成三角形。
36+
因为我们不能用任何三条边长来构成一个非零面积的三角形,所以我们返回 0。</pre>
37+
38+
<p>&nbsp;</p>
39+
40+
<p><strong>提示:</strong></p>
41+
42+
<ul>
43+
<li><code>3 &lt;= nums.length &lt;= 10<sup>4</sup></code></li>
44+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
45+
</ul>
46+
47+
48+
49+
## 解题方法:排序
50+
51+
将边长排序,采用贪心思维,先看最长边能不能用上:
52+
53+
> 选中所有边中的最长边,在此前提下,另外两边之和需要大于最长边。并且我们希望三角形周长最长,我们应该怎么选另外两条边?
54+
>
55+
> 很简单,不用动脑子就知道,另外两条边就选剩下的边中最长的两条。
56+
>
57+
> + 如果另外两边之和大于最长边,则完美,最长的三条边可以组成三角形,那么选这三条边所组成的三角形周长一定最长;
58+
>
59+
> + 否则,很遗憾,没有任何剩下的两条边之和可以大于第一条选中的最长边,可以说明最长的那条边一定不会被选中,直接扔掉最长边。
60+
61+
如果发现最长边不能被用上,扔掉最长边,剩下的所有边仍然按照这个思路去挑选就好。
62+
63+
+ 时间复杂度$O(n\log n)$,其中$n=len(nums)$
64+
+ 空间复杂度$O(\log n)$
65+
66+
### AC代码
67+
68+
#### C++
69+
70+
```cpp
71+
/*
72+
* @Author: LetMeFly
73+
* @Date: 2025-09-28 13:21:46
74+
* @LastEditors: LetMeFly.xyz
75+
* @LastEditTime: 2025-09-28 13:22:32
76+
*/
77+
class Solution {
78+
public:
79+
int largestPerimeter(vector<int>& nums) {
80+
sort(nums.begin(), nums.end());
81+
for (int i = nums.size() - 3; i >= 0; i--) {
82+
if (nums[i] + nums[i + 1] > nums[i + 2]) {
83+
return nums[i] + nums[i + 1] + nums[i + 2];
84+
}
85+
}
86+
return 0;
87+
}
88+
};
89+
```
90+
91+
#### Python
92+
93+
```python
94+
'''
95+
Author: LetMeFly
96+
Date: 2025-09-28 13:21:46
97+
LastEditors: LetMeFly.xyz
98+
LastEditTime: 2025-09-28 13:30:31
99+
'''
100+
from typing import List
101+
102+
class Solution:
103+
def largestPerimeter(self, nums: List[int]) -> int:
104+
nums.sort()
105+
for i in range(len(nums) - 3, -1, -1):
106+
if nums[i] + nums[i + 1] > nums[i + 2]:
107+
return nums[i] + nums[i + 1] + nums[i + 2]
108+
return 0
109+
```
110+
111+
#### Java
112+
113+
```java
114+
/*
115+
* @Author: LetMeFly
116+
* @Date: 2025-09-28 13:21:46
117+
* @LastEditors: LetMeFly.xyz
118+
* @LastEditTime: 2025-09-28 13:27:11
119+
*/
120+
import java.util.Arrays;
121+
122+
class Solution {
123+
public int largestPerimeter(int[] nums) {
124+
Arrays.sort(nums);
125+
for (int i = nums.length - 3; i >= 0; i--) {
126+
if (nums[i] + nums[i + 1] > nums[i + 2]) {
127+
return nums[i] + nums[i + 1] + nums[i + 2];
128+
}
129+
}
130+
return 0;
131+
}
132+
}
133+
```
134+
135+
#### Go
136+
137+
```go
138+
/*
139+
* @Author: LetMeFly
140+
* @Date: 2025-09-28 13:21:46
141+
* @LastEditors: LetMeFly.xyz
142+
* @LastEditTime: 2025-09-28 13:29:34
143+
*/
144+
package main
145+
146+
import "sort"
147+
148+
func largestPerimeter(nums []int) int {
149+
sort.Ints(nums)
150+
for i := len(nums) - 3; i >= 0; i-- {
151+
if nums[i] + nums[i + 1] > nums[i + 2] {
152+
return nums[i] + nums[i + 1] + nums[i + 2]
153+
}
154+
}
155+
return 0
156+
}
157+
```
158+
159+
#### Rust
160+
161+
```rust
162+
/*
163+
* @Author: LetMeFly
164+
* @Date: 2025-09-28 13:21:46
165+
* @LastEditors: LetMeFly.xyz
166+
* @LastEditTime: 2025-09-28 13:25:57
167+
*/
168+
impl Solution {
169+
pub fn largest_perimeter(mut nums: Vec<i32>) -> i32 {
170+
nums.sort();
171+
for i in (0..nums.len()-2).rev() {
172+
if nums[i] + nums[i + 1] > nums[i + 2] {
173+
return nums[i] + nums[i + 1] + nums[i + 2];
174+
}
175+
}
176+
0
177+
}
178+
}
179+
```
180+
181+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/152215094)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/09/28/LeetCode%200976.%E4%B8%89%E8%A7%92%E5%BD%A2%E7%9A%84%E6%9C%80%E5%A4%A7%E5%91%A8%E9%95%BF/)哦~
182+
>
183+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

0 commit comments

Comments
 (0)