15,题目:
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note: The solution set must not contain duplicate triplets.
For example, given array S = [-1, 0, 1, 2, -1, -4],
A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]
本题和TwoSum、FourSUM都是一个系列的。目的是寻找三个数和为0的子集。一种最直接的思路就是使用两个循环,外循环遍历整个数组,固定第一个元素,内循环进行TwoSum操作,找到和为0的组合即可。代码入下:
public List<List<Integer>> threeSum(int[] num) {
Arrays.sort(num);
List<List<Integer>> res = new ArrayList<>();
for (int i = 0; i < num.length-2; i++) {
if (i == 0 || (i > 0 && num[i] != num[i-1])) {
int lo = i+1, hi = num.length-1, sum = 0 - num[i];
while (lo < hi) {
if (num[lo] + num[hi] == sum) {
res.add(Arrays.asList(num[i], num[lo], num[hi]));
//跳过相等的元素
while (lo < hi && num[lo] == num[lo+1]) lo++;
while (lo < hi && num[hi] == num[hi-1]) hi--;
lo++; hi--;
} else if (num[lo] + num[hi] < sum) lo++;
else hi--;
}
}
}
return res;
}
另外一种思路是使用hashset来存储遍历过的组合,这种方法因为使用了比较复杂的数据结构,所以比较耗时,代码入下:
public List<List<Integer>> threeSum1(int[] num) {
Arrays.sort(num);
List<List<Integer>> list = new ArrayList<>();
HashSet<List<Integer>> set = new HashSet<>();
for(int i=0;i<num.length;i++)
{
for(int j=i+1,k=num.length-1;j<k;)
{
if(num[i]+num[j]+num[k]==0)
{
List<Integer> l= new ArrayList<Integer>();
l.add(num[i]);
l.add(num[j]);
l.add(num[k]);
if(set.add(l))
list.add(l);
j++;
k--;
}
else if(num[i]+num[j]+num[k]<0)
j++;
else
k--;
}
}
return list;
}
16, 题目:
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
For example, given array S = {-1 2 1 -4}, and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
本题是上面题目的变种,目的是寻找和最接近target的三个数。我们直接可以使用上面的思路,不过定义一个变量来保存结果和最小的差。代码如下:
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
int diff = Integer.MAX_VALUE, closest = 0;
for (int k=0; k<nums.length-2; ++k) {
for (int i=k+1, j=nums.length-1; i<j; ) {
int sum = nums[k] + nums[i] + nums[j];
if (sum == target) { return target; }
else if (sum > target) {
if (sum-target < diff) {
diff = sum-target;
closest = sum;
}
--j;
} else {
if (target-sum < diff) {
diff = target-sum;
closest = sum;
}
++i;
}
}
}
return closest;
}