454.四数相加II
题目
给定四个包含整数的数组列表 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 。所有整数的范围在 -2^28 到 2^28 - 1 之间,最终结果不会超过 2^31 - 1 。
思路
用map统计a,b和的次数,然后再用0-c,d和在map中查找,满足条件的统计value.
代码实现
class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
// 新建一个map
Map<Integer,Integer> map = new HashMap<>();
// 遍历A,B 存储AB的和为key,value为和出现的次数
int count = 0;
for(int a:nums1){
for(int b:nums2){
int sumAB = a + b;
if(map.containsKey(sumAB)){
map.put(sumAB,map.get(sumAB)+1);
}else{
map.put(sumAB,1);
}
}
}
// 遍历C,D,同时在map中查找0-(C+D)
// 如果找到了count++
for(int c:nums3){
for(int d:nums4){
int sumCD = c + d;
if(map.containsKey(0-sumCD)){
count += map.get(0-sumCD);
}
}
}
return count;
}
}
383. 赎金信
题目
给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串 ransom 能不能由第二个字符串 magazines 里面的字符构成。如果可以构成,返回 true ;否则返回 false。
思路
字母异位词的翻版题目,可用相同的方式解决
代码
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
// 与字母异位词类似的做法
// 定义一个哈希映射数组
int[] record = new int[26];
// 遍历magazine,统计每个字符出现的次数
for(int i=0;i<magazine.length();i++){
record[magazine.charAt(i)-'a'] ++;
}
// 遍历ransomNote,减去每个字符出现的次数
for(int i =0;i<ransomNote.length();i++){
record[ransomNote.charAt(i)-'a']--;
}
// 检查数组是否有元素小于0,如果有返回false
for(int s: record){
if(s<0){
return false;
}
}
return true;
}
}
15. 三数之和
题目
给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组。
思路
哈希法
哈希法的核心思路与求解二数之和的相同,先求出任意两个数组的和,再看做求二数和的问题。
但要注意需要去重处理。哈希法因为时间复杂度至少为O(n^2),而且用到了map数据结构很费时。不推荐使用,仅仅提供一种可能的思路。
双指针
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) {
return result;
}
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
int left = i + 1;
int right = nums.length - 1;
while (right > left) {
int sum = nums[i] + nums[left] + nums[right];
if (sum > 0) {
right--;
} else if (sum < 0) {
left++;
} else {
result.add(Arrays.asList(nums[i], nums[left], nums[right]));
while (right > left && nums[right] == nums[right - 1]) right--;
while (right > left && nums[left] == nums[left + 1]) left++;
right--;
left++;
}
}
}
return result;
}
}
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] ]
思路
代码
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(nums);
for (int i = 0; i < nums.length; i++) {
// nums[i] > target 直接返回, 剪枝操作
if (nums[i] > 0 && nums[i] > target) {
return result;
}
if (i > 0 && nums[i - 1] == nums[i]) {
continue;
}
for (int j = i + 1; j < nums.length; j++) {
if (j > i + 1 && nums[j - 1] == nums[j]) {
continue;
}
int left = j + 1;
int right = nums.length - 1;
while (right > left) {
long sum = (long) nums[i] + nums[j] + nums[left] + nums[right];
if (sum > target) {
right--;
} else if (sum < target) {
left++;
} else {
result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
while (right > left && nums[right] == nums[right - 1]) right--;
while (right > left && nums[left] == nums[left + 1]) left++;
left++;
right--;
}
}
}
}
return result;
}
}