/**
* Runtime: 3 ms, faster than 99.71%
* Memory Usage: 38.5 MB, less than 67.37%
*/
class Solution {
public int threeSumSmaller(int[] nums, int target) {
Arrays.sort(nums);
int res = 0;
for (int i = 0; i < nums.length - 2; i++) {
int remain = target - nums[i], left = i + 1, right = nums.length - 1;
if (remain <= 0 && nums[left] > 0) {
break;
}
while (left < right) {
if (nums[left] + nums[right] >= remain) {
right--;
} else {
res += right - left;
left++;
}
}
}
return res;
}
}
259. 3Sum Smaller [Medium]
最新推荐文章于 2024-05-31 17:17:25 发布