1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 | public class Solution {
public int threeSumSmaller(int[] nums, int target) {
if (nums == null || nums.length == 0) return 0;
Arrays.sort(nums);
int total = 0;
for (int i = 0; i < nums.length-1; i++){
total += twoSumSmaller(nums, i+1, target-nums[i]);
}
return total;
}
public int twoSumSmaller(int []nums, int start, int target){
int total = 0;
int left = start;
int right = nums.length-1;
while (left < right){
if (nums[left]+nums[right] < target){
total+=(right-left);
left++;
}else{
right--;
}
}
return total;
}
}
|
No comments:
Post a Comment