259 3Sum Smaller
259. 3Sum Smaller
1. Question
[-2, 0, 1]
[-2, 0, 3]2. Implementation
class Solution {
public int threeSumSmaller(int[] nums, int target) {
int res = 0;
Arrays.sort(nums);
for (int i = 0; i < nums.length - 2; i++) {
int j = i + 1, k = nums.length - 1;
while (j < k) {
int sum = nums[i] + nums[j] + nums[k];
if (sum < target) {
res += k - j;
++j;
}
else {
--k;
}
}
}
return res;
}
}3. Time & Space Complexity
Last updated