Array
3Sum
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
for (int i = 0; i < nums.length && nums[i] <= 0; ++i) {
if (i == 0 || nums[i-1] != nums[i]) {
twoSumII(i, nums, res);
}
}
return res;
}
void twoSumII(int i, int[] nums, List<List<Integer>> res) {
int low = i + 1, high = nums.length - 1;
while (low < high) {
int sum = nums[i] + nums[low] + nums[high];
if ( sum > 0) {
--high;
} else if (sum < 0) {
++low;
} else {
res.add(Arrays.asList(nums[i], nums[low++], nums[high--]));
while (low < high && nums[low] == nums[low - 1]) {
++low;
}
}
}
}
}3Sum Closest
Product of Array Except Self
Kids With the Greatest Number of Candies
Increasing Triplet Subsequence
Last updated