JAVA O(n^2)


  • 0
    G
    public class Solution {
        public int triangleNumber(int[] nums) {
            int ret = 0;
            Arrays.sort(nums);
            for (int i = 0; i < nums.length - 2; i++) {
                int slow = i + 1;
                int fast = i + 2;
                while (fast < nums.length) {
                    if (slow == fast) {
                        fast++;
                    } else if (nums[slow] + nums[i] <= nums[fast]) {
                        slow++;
                    } else {
                        ret += fast - slow;
                        fast++;
                    }
                }
            }
            return ret;
        }
    }
    

Log in to reply
 

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