数字之和问题
1. 两数之和
给定一个整数数组 nums 和一个整数目标值 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]
示例 3:
输入:nums = [3,3], target = 6
输出:[0,1]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/two-sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路: 使用HashMap的key存储目标的差,并记录它的位置。等遍历到目标差时就找到匹配的值。
class Solution { public int[] twoSum(int[] nums, int target) { int[] result = new int[2]; Mapfor(int i = 0; i < nums.length; i++) { if(numMap.containsKey(nums[i])) { int index = numMap.get(nums[i]); result[0] = index; result[1] = i; break; }
numMap.put(target - nums[i], i);
}
return result; } }
2. 三数之和
给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有和为 0 且不重复的三元组。
注意:答案中不可以包含重复的三元组。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/3sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:注意点,不可以出现重复三元组。 先排序,在进行遍历。有重复的值跳过。
class Solution { public List- > threeSum(int[] nums) {
List
- > result = new ArrayList<>();
int len = nums.length;
if(len < 3) {
return result;
}
Arrays.sort(nums);
for(int first = 0; first < len; first++) { if(first > 0 && nums[first] == nums[first-1]) { continue; } int third = len - 1; int target = -nums[first];
for(int second = first + 1; second < len; second++) { if(second > first + 1 && nums[second] == nums[second - 1]) { continue; } while(second < third && (nums[second] + nums[third]) > target) { third--; }
if(second == third) { continue; }
if(nums[second] + nums[third] == target) { List
return result; } }
3. 四数之和
给定一个包含 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]
]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/4sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:此题和三数相加类似, 通过剪枝和双指针可以去除不必要的相加。
class Solution { public List- > fourSum(int[] nums, int target) {
List
- > result = new ArrayList<>();
if(nums == null || nums.length < 4) {
return result;
}
int len = nums.length; Arrays.sort(nums);
for(int first = 0; first < len - 3; first++) { if(first > 0 && nums[first] == nums[first-1]) { continue; }
if(nums[first] + nums[first + 1] + nums[first + 2] + nums[first + 3] > target) { break; }
if(nums[first] + nums[len - 1] + nums[len - 2] + nums[len -3] < target) { continue; }
for(int second = first + 1; second < len - 2; second++) { if(second > first + 1 && nums[second] == nums[second - 1]) { continue; } if(nums[first] + nums[second] + nums[second + 1] + nums[second + 2] > target) { break; } if(nums[first] + nums[second] + nums[len - 1] + nums[len -2] < target) { continue; }
int left = second + 1, right = len - 1;
while(left < right) { int sum = nums[first] + nums[second] + nums[left] + nums[right]; if(sum < target) { left++; } else if (sum > target) { right--; } else { List
while(left < right && nums[right] == nums[right-1]) { right--; } right--; while(left < right && nums[left] == nums[left + 1]) { left++; } left++; } } } } return result; } }