Java AC, just another pointer direction which I think more intuitive.


  • 1
    C
    public class Solution {
        public int threeSumSmaller(int[] nums, int target) {
            if(nums == null || nums.length < 3) return 0;
            int count = 0;
            Arrays.sort(nums);
            for(int i = 0; i < nums.length; i++){
                int l = i + 1, r = nums.length - 1;
                while(l < r){
                    int sum = nums[l] + nums[r] + nums[i];
                    if(sum < target) {
                        count += r - l;
                        l++;
                    }
                    else r--;
                }
            }
            return count;
        }
    }

  • 0
    S
    count += hi - lo
    

    why there is no duplicate case because you add for several times? Besides, if l(left)+1 is larger than target, you just cannot add this interval to the count of the final result. Could you elaborate it?


Log in to reply
 

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