给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
示例:
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
思路:
用一个Map存储,先map.get 不在就Put进去.
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer,Integer> map = new HashMap<>();
for(int i=0;i<nums.length;i++){
if(map.containsKey(target-nums[i])){
return new int[]{map.get(target-nums[i]),i};
}
map.put(nums[i],i);
}
return null;
}
}
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。
注意:答案中不可以包含重复的三元组。
例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4],
满足要求的三元组集合为:
[
[-1, 0, 1],
[-1, -1, 2]
]
三数之和
思路: for 外层循环,再内2层双指针
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
if (nums == null || nums.length < 3 )
return list;
Arrays.sort(nums);
int a,b,c;
for (a = 0;a < nums.length-2;a++) {
b = a+1;
c = nums.length-1;
if (a > 0 && nums[a] == nums[a-1]) {
continue;
}
int max1 = nums[a]+nums[nums.length-1]+nums[nums.length-2];
if (max1 < 0)
continue;
while (b < c) {
int sum = nums[a]+nums[b]+nums[c];
if (0 == sum) {
list.add(Arrays.asList(nums[a],nums[b],nums[c]));
b++;
while (b < nums.length-1 && nums[b] == nums[b-1])
{
b++;
}
c--;
while (c > b && nums[c] == nums[c+1]) {
c--;
}
} else if (sum > 0) {
c--;
} else {
b++;
}
}
}
return list;
}
}
给定一个包含 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]
]
思路:同上,for2层再双指针
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> list = new ArrayList<>();
if (nums == null || nums.length < 4) {
return list;
}
Arrays.sort(nums);
int a,b,c,d;
int ss = 0;
for ( a = 0;a < nums.length-3;a++) {
if (a > 0 && nums[a] == nums[a-1]) {
continue;
}
// 剪枝 提速
int mi1 = nums[0]+nums[1]+nums[2]+nums[3];
if (mi1 > target)
break;
// 剪枝 提速
int max1 = nums[nums.length-1]+nums[nums.length-2]+nums[nums.length-3]+nums[a];
if (max1 < target)
continue;
for (b = a+1;b < nums.length-2;b++) {
if (b > a+1 && nums[b] == nums[b-1])
continue;
c = b+1;
d = nums.length-1;
int min1 = nums[a]+nums[b]+nums[c]+nums[c+1];
if (min1 > target)
continue;
int max2 = nums[a]+nums[b]+nums[nums.length-1]+nums[nums.length-2];
if (max2 < target)
continue;
ss = nums[a] + nums[b];
while (c < d) {
int sum = ss+nums[c]+nums[d];
if (target == sum) {
list.add(Arrays.asList(nums[a],nums[b],nums[c],nums[d])); c++;
// 剪枝 nums[c] == nums[c+1]
while (c < nums.length-1 && nums[c]==nums[c-1])
{
c++;
}
// 剪枝学习
d--;
while (d > c && nums[d] == nums[d+1]) {
d--;
}
}
else if (target > sum) {
c++;
} else if (target < sum) {
d--;
}
}
}
}
return list;
}
}
给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0。
为了使问题简单化,所有的 A, B, C, D 具有相同的长度 N,且 0 ≤ N ≤ 500 。所有整数的范围在 -228 到 228 - 1 之间,最终结果不会超过 231 - 1 。
例如:
输入:
A = [ 1, 2]
B = [-2,-1]
C = [-1, 2]
D = [ 0, 2]
输出:
2
解释:
两个元组如下:
- (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0
- (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0
思路:
map存在2个集合的元素
再get相反数即可
class Solution {
public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
int res = 0;
if (A == null || A.length == 0)
return res;
Map<Integer,Integer> map = new HashMap<>();
for (int a:A)
for (int b : B) {
int ss = a+b;
map.put(ss,map.getOrDefault(ss,0)+1);
}
for (int c:C)
for(int d:D) {
int nn = c+d;
res += map.getOrDefault(-1*(nn),0);
}
return res;
}
}