1. 两数之和
给定一个整数数组 nums
和一个整数目标值 target
,请你在该数组中找出 和为目标值 target
的那 两个 整数,并返回它们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
你可以按任意顺序返回答案。只会存在一个有效答案。
示例 1:
输入:nums = [2,7,11,15], target = 9 输出:[0,1] 解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
示例 2:
输入:nums = [3,2,4], target = 6 输出:[1,2]
HashMap解法
- 数组的大小是受限制的,而且如果元素很少,而哈希值太大会造成内存空间的浪费。
- set是一个集合,里面放的元素只能是一个key,而两数之和这道题目,不仅要判断y是否存在而且还要记录y的下标位置,因为要返回x 和 y的下标。所以set 也不能用。
选择另一种数据结构:map ,map是一种key value的存储结构,可以用key保存数值,用value再保存数值所在的下标
class Solution {
public int[] twoSum(int[] nums, int target) {
if(nums.length == 0 || nums == null){//保证代码的健壮性
return res;
}
int[] res = new int [2]; // 存入结果
Map<Integer,Integer> map = new HashMap<>();
for(int i =0; i < nums.length ; i++){
int tem = target - nums[i]; //寻找目标值
if(map.containsKey(tem)){
res[0]=i;
res[1]=map.get(tem);
return res;
}
map.put(nums[i],i);
}
return null;
}
}
454.四数相加II
给你四个整数数组 nums1
、nums2
、nums3
和 nums4
,数组长度都是 n
,请你计算有多少个元组 (i, j, k, l)
能满足:
0 <= i, j, k, l < n
nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0
示例 1:
输入:nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] 输出:2 解释: 两个元组如下: 1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
示例 2:
输入:nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] 输出:1
HashMap解法
本题是使用哈希法的经典题目,而0015.三数之和 (opens new window),0018.四数之和 (opens new window)并不合适使用哈希法
前两个数之和等于负数(后两个数之和)
class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
int res =0;
//键:数之和 值:出现的次数
Map<Integer,Integer> map = new HashMap<>();
for(int i:nums1){
for(int j:nums2){ //先考虑前两个数组
int sum = i+j;
map.put(sum,map.getOrDefault(sum,0)+1);
}
}
for(int i:nums3){
for(int j:nums4){
int sum = i+j;
res += map.getOrDefault(-sum,0); //前两个数之和等于负数(后两个数之和)
}
}
return res;
}
}
难度升级:就是给出一个数组(而不是四个数组),在这里找出四个元素相加等于0,答案中不可以包含重复的四元组(类似于三数之和)
15.三数之和
给你一个整数数组 nums
,判断是否存在三元组 [nums[i], nums[j], nums[k]]
满足 i != j
、i != k
且 j != k
,同时还满足 nums[i] + nums[j] + nums[k] == 0
。请
你返回所有和为 0
且不重复的三元组。
注意:答案中不可以包含重复的三元组。
示例 1:
输入:nums = [-1,0,1,2,-1,-4] 输出:[[-1,-1,2],[-1,0,1]] 解释: nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0 。 nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0 。 nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0 。 不同的三元组是 [-1,0,1] 和 [-1,-1,2] 。 注意,输出的顺序和三元组的顺序并不重要。
示例 2:
输入:nums = [0,1,1] 输出:[] 解释:唯一可能的三元组和不为 0 。
-10^5 <= nums[i] <= 10^5
双指针解法
题目难点:题目要求去重,索引不能重复,三元组里的元素可以重复,但三元组不可重复
题目不要求返回下标,可用排序
可以通过 nums[i] > 0
就退出循环了,因为 0 已经是确定的数了
一层循环确定第一个数,后两个数用双指针
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(nums);
for ( int i = 0; i < nums.length;i++){
if (nums[i] > 0 ){ // 排序后第一个就1大于零,就凑不出等于零
break;
}
if(i>0 && nums[i]==nums[i-1]){
continue; //去重a
}
int left = i + 1;
int right = nums.length - 1;
while(left <right){ //双指针
if (nums[i]+nums[left]+nums[right]>0)right--; //right左移
else if (nums[i]+nums[left]+nums[right]<0)left++; //left右移
else{
result.add(Arrays.asList(nums[i],nums[left],nums[right]));
//先添加后去重,以防特殊情况(全是零)
while (left < right && nums[left] == nums[left+1])left++;//去重left
while (left < right && nums[right] == nums[right-1])right--;//去重right
left++;
right--;
}
}
}
return result;
}
}
错误例子:
while(left <right){ //双指针
if (nums[i]+nums[left]+nums[right]>0)right--; //right左移
if (nums[i]+nums[left]+nums[right]<0)left++; //left右移
if (nums[i]+nums[left]+nums[right]<0){
result.add(Arrays.asList(nums[i],nums[left],nums[right]));
//先添加后去重,以防特殊情况(全是零)
while (left < right && nums[left] == nums[left+1])left++;//去重left
while (left < right && nums[right] == nums[right-1])right--;//去重right
left++;
right--;
}
}
表面上是用三个if来包括了所有的结果,跟if-else等同
实际有个严重错误:第一个if实施完后不会跳入下一个循环,而是影响下一个if的判断条件,从而一个一次循环会进入多个if中,与设想每次循环只判断一次相违。最好用上if-else语句
18.四数之和
给你一个由 n
个整数组成的数组 nums
,和一个目标值 target
。请你找出并返回满足下述全部条件且不重复的四元组 [nums[a], nums[b], nums[c], nums[d]]
(若两个四元组元素一一对应,则认为两个四元组重复):
0 <= a, b, c, d < n
a
、b
、c
和d
互不相同nums[a] + nums[b] + nums[c] + nums[d] == target
你可以按 任意顺序 返回答案 。
示例 1:
输入:nums = [1,0,-1,0,-2,2], target = 0 输出:[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
示例 2:
输入:nums = [2,2,2,2,2], target = 8 输出:[[2,2,2,2]]
-10^9 <= nums[i] <= 10^9
双指针解法
454.四数相加II 相对于本题简单很多,因为本题是要求在一个集合中找出四个数相加等于target,同时四元组不能重复。
454.四数相加II 是四个独立的数组,只要找到A[i] + B[j] + C[k] + D[l] = 0就可以,不用考虑有重复的四个元素相加等于0的情况,
用双层循环来确定第一和第二个数,后两个数用双指针,思路跟三数之和一样
但此题是不要求等于零,而是等于target,所以不能用首元素大于零直接跳出循环
注意:此题用int会溢出,应使用long
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(nums);
for(int j = 0; j <nums.length -3;j++){
if(j>0 && nums[j]==nums[j-1]){
continue; //去重a
}
for ( int i = j+1 ; i < nums.length-2;i++){
if(i != j+1 && nums[i]==nums[i-1]){
continue; //去重b
}
int left = i + 1;
int right = nums.length - 1;
while(left <right){ //双指针 c 和 d
long sum = (long)nums[i]+nums[j]+nums[left]+nums[right];
if (sum>target)right--; //right左移
else if (sum<target)left++; //left右移
else{
result.add(Arrays.asList(nums[j],nums[i],nums[left],nums[right]));
//先添加后去重,以防特殊情况(全是零)
while (left < right && nums[left] == nums[left+1])left++;//去重left
while (left < right && nums[right] == nums[right-1])right--;//去重right
left++;
right--;
}
}
}
}
return result;
}
}