1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public int threeSumSmaller(int[] nums, int target) {
Arrays.sort(nums);
int ans = 0;
for (int i = 0; i < nums.length - 2; i++) {
int left = i + 1, right = nums.length - 1;
while (left < right) {
if (nums[left] + nums[right] + nums[i] < target) {
ans += right - left;
left += 1;
} else {
right -= 1;
}
}
}
return ans;
}
}

two pointers