976. 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:
Input: [2,1,2]
Output: 5
Example 2:
Input: [1,2,1]
Output: 0
Example 3:
Input: [3,2,3,4]
Output: 10
Example 4:
Input: [3,6,2,3]
Output: 8
Note:
- 3 <= A.length <= 10000
- 1 <= A[i] <= 10^6
题目大意 #
找到可以组成三角形三条边的长度,要求输出三条边之和最长的,即三角形周长最长。
解题思路 #
这道题也是排序题,先讲所有的长度进行排序,从大边开始往前找,找到第一个任意两边之和大于第三边(满足能构成三角形的条件)的下标,然后输出这 3 条边之和即可,如果没有找到输出 0 。
代码 #
package leetcode
func largestPerimeter(A []int) int {
if len(A) < 3 {
return 0
}
quickSort164(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
}