D62 976. Largest Perimeter Triangle

Topic link

976. Largest Perimeter Triangle

Subject analysis

Given an array of numbers, take any three sides to form a triangle and return the maximum side length.

Train of thought

Sort the given array in descending order so that the largest number comes first.

Take the first three of the largest and determine whether the sum of any two sides is greater than the third side. If yes, return the perimeter.

The final code


      
class Solution {

    / * * *@param Integer[] $A
     * @return Integer
     */
    function largestPerimeter($A) {
        rsort($A);
        $length = count($A);
        for($i = 0; $i<$length2 -; $i++){
            if(  ($A[$i]   + $A[$i+1] > $A[$i+2])
              && ($A[$i]   + $A[$i+2] > $A[$i+1])
              && ($A[$i+1] + $A[$i+2] > $A[$i])
              ){
                return $A[$i] + $A[$i+1] + $A[$i+2]; }}return 0; }}Copy the code

If you find this article useful, you are welcome to subsidize it with love.