#leetCode brush title documentary Day24

https://leetcode-cn.com/problems/largest-perimeter-triangle/

A given array by a number of positive (representative length) consisting of return, the area of ​​the triangle is not zero from the maximum perimeter length of which three thereof.

If not form any non-zero triangular area, 0 is returned.

 

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
 

prompt:

3 <= A.length <= 10000
1 <= A[i] <= 10^6

 

Chicken dishes to try:

       Harm, wrote so many days or only write the most brutal. To write a triangle determination function determines whether the area is not the composition of the triangle 0. Title area is not required to give up a triangular perimeter zero, then the priority of the three sides of the longest, if not form a non-zero area triangle, consider three times longer ......

       Thus producing ideas, give side descending order sets, each of three sides of three border areas traversed, once satisfied triangular area appears to nonzero outputs circumference. If the end of the cycle has not been out, returns to 0 (that area of ​​the non composition absent the three sides of the triangle 0)

 1 class Solution {
 2 public:
 3     int judge (int a, int b, int c) {
 4         if (a + b > c && a + c > b & b + c > a) return 1;
 5         return 0;
 6     }
 7     int largestPerimeter(vector<int>& A) {
 8         sort(A.begin(), A.end(), greater<int>());
 9         int size = A.size();
10         for (int i = 0; i < size - 2; i ++) {
11             if (judge(A[i], A[i + 1], A[i + 2]) == 0) continue;
12             return A[i] + A[i + 1] + A[i + 2];
13         } 
14         return 0;
15     }
16 };

 

Worship Gangster Code:

Read the solution to a problem seems to still do it! Yeah yeah!

 

 

 

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/largest-perimeter-triangle
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

Guess you like

Origin www.cnblogs.com/xyy999/p/11920521.html