Easy 16 lines of Java code with O(N2) self-explain


  • 0
    C
    
    public class Solution {
        public int threeSumSmaller(int[] nums, int target) {
            Arrays.sort(nums);
            int total = 0;
            for(int i=0; i<nums.length;i++) {
                int j = i+1;
                int k = nums.length -1;
                while(j<k) {
                    if(nums[i]+nums[j]+nums[k] < target) {
                        total += (k-j);
                        j++;
                    } else {
                        k--;
                    }
                }
            }
            return total;
        }
    }
    
    

Log in to reply
 

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