leet-code/src/main/java/leetcode/editor/cn/LargestPerimeterTriangle.java

71 lines
1.4 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//给定由一些正数(代表长度)组成的数组 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)
}