259. 3Sum Smaller

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

}

猜你喜欢

转载自www.cnblogs.com/tobeabetterpig/p/9327151.html
今日推荐