数字之和
15. 三数之和
20200612
难度:中等
题目描述
给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组。
注意:答案中不可以包含重复的三元组。
示例:
给定数组 nums = [-1, 0, 1, 2, -1, -4],
满足要求的三元组集合为:
[
[-1, 0, 1],
[-1, -1, 2]
]
Solution
排序+双指针,注意去重
- 先对数组排序
- 固定一个数
nums[i]
,在用左右指针L
和R
在nums[i]
的后面分别查找- 若
nums[i] > 0
,则三个数的和一定大于0,直接break
- 若
nums[i] == nums[i-1]
,说明数字重复,跳过 - 计算三个数的和
sum = nums[i] + nums[L] + nums[R]
- 若
sum == 0
,符合要求,写入结果,并更新下一个L
和R
- 若
sum < 0
,说明数字不够大,L
右移 - 若
sum > 0
,R
左移
- 若
- 若
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
Arrays.sort(nums);
int len = nums.length;
//固定一个nums[i],移动L和R查找
for(int i = 0; i < len-2; i++){
if(nums[i] > 0){
break;
}
//去重
if(i > 0 && nums[i] == nums[i-1]){
continue;
}
int L = i+1;
int R = len-1;
while(L < R){
int sum = nums[i] + nums[L] + nums[R];
//符合要求,写入结果
if(sum == 0){
ans.add(Arrays.asList(nums[i], nums[L], nums[R]));
while(L < R && nums[L] == nums[L+1]){
L++;
}
while(L < R && nums[R] == nums[R-1]){
R--;
}
L++;
R--;
}else if(sum < 0){
L++;
}else{
R--;
}
}
}
return ans;
}
}
其他 数之和 的相关题目:
1. 两数之和
难度:简单
题目描述
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。
示例:
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
Solution
哈希表
class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> map = new HashMap<>();
int[] res = new int[2];
for(int i = 0; i < nums.length; i++){
int dif = target - nums[i];
if(map.get(dif) != null){
res[0] = map.get(dif);
res[1] = i;
}
map.put(nums[i],i);
}
return res;
}
}
18. 四数之和
难度:中等
题目描述
给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。
注意:
答案中不可以包含重复的四元组。
示例:
给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0。
满足要求的四元组集合为:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]
Solution
排序+双指针+值域判断
- 在三数之和外面多嵌套一层循环,就变成了四数字之和
- 加上值域判断:如果当前最小值比target大,跳过;如果当前最大值比target小,跳过。
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> ans = new ArrayList<>();
int len = nums.length;
if(nums == null || len < 4){
return ans;
}
Arrays.sort(nums);
for(int i = 0; i < len-3; i++){
if(i > 0 && nums[i] == nums[i-1]) continue;//去重
//获取当前最小值,如果最小值比target大,说明后面越来越大的也不可能,直接退出
int min1 = nums[i] + nums[i+1] + nums[i+2] + nums[i+3];
if(min1 > target) break;
//获取当前最小值,如果最大值比target小,说明后面越来越小的也不可能,直接进行下一个i的判断
int max1 = nums[i] + nums[len-1] + nums[len-2] + nums[len-3];
if(max1 < target) continue;
for(int j = i+1; j < len-2; j++){
if(j > i+1 && nums[j] == nums[j-1]) continue;//去重
int L = j + 1;
int R = len - 1;
//当前最小值,如果最小值比target大,跳过
int min2 = nums[i] + nums[j] + nums[L] + nums[L+1];
if(min2 > target) continue;
//当前最大值,如果最大值比target小,跳过
int max2 = nums[i] + nums[j] + nums[R] + nums[R-1];
if(max2 < target) continue;
while(L < R){
int sum = nums[i] + nums[j] + nums[L] + nums[R];
if(sum == target){
ans.add(Arrays.asList(nums[i], nums[j], nums[L], nums[R]));
while(L < R && nums[L] == nums[L+1]) L++;//去重
while(L < R && nums[R] == nums[R-1]) R--;//去重
L++;
R--;
}else if(sum < target){
L++;
}else if(sum > target){
R--;
}
}
}
}
return ans;
}
}
16. 最接近的三数之和
难度:中等
题目描述
给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数,使得它们的和与 target 最接近。返回这三个数的和。假定每组输入只存在唯一答案。
示例:
输入:nums = [-1,2,1,-4], target = 1
输出:2
解释:与 target 最接近的和是 2 (-1 + 2 + 1 = 2) 。
Solution
双指针,和三数之和不同的是 要改成和target
的差的绝对值判断
class Solution {
public int threeSumClosest(int[] nums, int target) {
int len = nums.length;
Arrays.sort(nums);
int sum = nums[0] + nums[1] + nums[2];
for(int i = 0; i < len-2; i++){
int l = i+1;
int r = len-1;
while(l < r){
int cur = nums[i] + nums[l] + nums[r];
if(Math.abs(cur - target) < Math.abs(sum - target)){
sum = cur;
}
if(cur > target){
r--;
}else if(cur < target){
l++;
}else{
return target;
}
}
}
return sum;
}
}
欢迎关注公众号:GitKid,暂时每日分享LeetCode题解,在不断地学习中,公众号内容也在不断充实,欢迎扫码关注