Shawngbk / Leecode

Questions of Leecode
0 stars 0 forks source link

259. 3Sum Smaller #173

Open Shawngbk opened 7 years ago

Shawngbk commented 7 years ago

public class Solution { public int threeSumSmaller(int[] nums, int target) { Arrays.sort(nums); int count = 0; for(int i = 0; i < nums.length - 2; i++) { //每个triple不是unique,可以重复 int lo = i+1, hi = nums.length - 1; while(lo < hi) { if(nums[lo] + nums[i] + nums[hi] < target) {//所有小于nums[hi]的都满足条件 count += hi - lo; lo++; } else { hi--; } }

    }
    return count;
}

}