37ms Java simple O(n^2) solution beat 98%


  • 0
    S
    public int triangleNumber(int[] nums) {
            if(nums.length < 3) return 0;
            Arrays.sort(nums);
            int count = 0;
            for(int i = 2; i < nums.length; i++){
                int l = 0;
                int r = i - 1;
                while(l < r){
                    while(l < r && nums[l] + nums[r] <= nums[i]) l++;
                    if(l < r) count += r - l;
                    r--;
                }
            }
            return count;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.