How to know that a triangle triple exists in our array?

后端 未结 13 1398
[愿得一人]
[愿得一人] 2020-12-04 16:52

I was stuck in solving the following interview practice question:
I have to write a function:

int triangle(int[] A);

that given a zero-

13条回答
  •  渐次进展
    2020-12-04 17:25

    PHP Solution :

    function solution($x){
        sort($x);
        if (count($x) < 3) return 0; 
        for($i = 2; $i < count($x); $i++){
            if($x[$i] < $x[$i-1] + $x[$i-2]){
                return 1;
            }
        }
    
        return 0;
    }
    

提交回复
热议问题