976:三角形的最大周长
This commit is contained in:
parent
b031bc8980
commit
8b1b060a27
@ -0,0 +1,71 @@
|
|||||||
|
//给定由一些正数(代表长度)组成的数组 A,返回由其中三个长度组成的、面积不为零的三角形的最大周长。
|
||||||
|
//
|
||||||
|
// 如果不能形成任何面积不为零的三角形,返回 0。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
// 输入:[2,1,2]
|
||||||
|
//输出:5
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
// 输入:[1,2,1]
|
||||||
|
//输出:0
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 3:
|
||||||
|
//
|
||||||
|
// 输入:[3,2,3,4]
|
||||||
|
//输出:10
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 4:
|
||||||
|
//
|
||||||
|
// 输入:[3,6,2,3]
|
||||||
|
//输出:8
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 3 <= A.length <= 10000
|
||||||
|
// 1 <= A[i] <= 10^6
|
||||||
|
//
|
||||||
|
// Related Topics 贪心 数组 数学 排序 👍 147 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
//976:三角形的最大周长
|
||||||
|
class LargestPerimeterTriangle {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new LargestPerimeterTriangle().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public int largestPerimeter(int[] nums) {
|
||||||
|
Arrays.sort(nums);
|
||||||
|
int size = nums.length;
|
||||||
|
for (int i = size; i >= 3; i--) {
|
||||||
|
if (nums[i - 3] + nums[i - 2] > nums[i - 1]) {
|
||||||
|
return nums[i - 3] + nums[i - 2] + nums[i - 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
<p>给定由一些正数(代表长度)组成的数组 <code>A</code>,返回由其中三个长度组成的、<strong>面积不为零</strong>的三角形的最大周长。</p>
|
||||||
|
|
||||||
|
<p>如果不能形成任何面积不为零的三角形,返回 <code>0</code>。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<ol>
|
||||||
|
</ol>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong>[2,1,2]
|
||||||
|
<strong>输出:</strong>5
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong>[1,2,1]
|
||||||
|
<strong>输出:</strong>0
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 3:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong>[3,2,3,4]
|
||||||
|
<strong>输出:</strong>10
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 4:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong>[3,6,2,3]
|
||||||
|
<strong>输出:</strong>8
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ol>
|
||||||
|
<li><code>3 <= A.length <= 10000</code></li>
|
||||||
|
<li><code>1 <= A[i] <= 10^6</code></li>
|
||||||
|
</ol>
|
||||||
|
<div><div>Related Topics</div><div><li>贪心</li><li>数组</li><li>数学</li><li>排序</li></div></div><br><div><li>👍 147</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user