目录
*454.四数相加II
四个数组里面寻找四个数相加和为0.巧妙的去利用hashmap判断是否存在的功能,hashmap1里面放前两个数组的和,以及这个和出现的次数
hashmap2里面放后两个数组的和(或者和的相反数)----->判断是否存在于hashmap1里面。
最后次数相乘后累加即为结果
class Solution {
/**
* 四数相加----->输出和为0的元组个数
* 两个两个的放
*
* @param nums1
* @param nums2
* @param nums3
* @param nums4
* @return
*/
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
HashMap<Integer, Integer> hashMap1 = new HashMap<>();//nums1和nums2里面元素和<和,次数>
HashMap<Integer, Integer> hashMap2 = new HashMap<>();//-(c+d)
for (int i = 0; i < nums1.length; i++) {
for (int j = 0; j < nums2.length; j++) {
int temp = nums1[i] + nums2[j];
if (hashMap1.containsKey(temp)) {
int cnt = hashMap1.get(temp) + 1;
hashMap1.put(temp, cnt);
} else {
hashMap1.put(temp, 1);
}
}
}
for (int i = 0; i < nums3.length; i++) {
for (int j = 0; j < nums4.length; j++) {
int temp = -(nums3[i] + nums4[j]);
if (hashMap2.containsKey(temp)) {
int cnt = hashMap2.get(temp) + 1;
hashMap2.put(temp, cnt);
} else {
hashMap2.put(temp, 1);
}
}
}
int ans = 0;
for (Map.Entry<Integer, Integer> entry : hashMap2.entrySet()) {//遍历hashmap2
if (hashMap1.containsKey(entry.getKey())) {
ans += hashMap1.get(entry.getKey()) * entry.getValue();
}
}
return ans;
}
}
383.赎金信
判断ransomNote是否是magazine的子集---->map2里面可以找到所有map1里面的值
并且map2里面的字符出现的次数必须>=map1中出现的次数
class Solution {
/**
* 判断ransomNote是否是magazine的子集---->map2里面可以找到所有map1里面的值
*
* @param ransomNote
* @param magazine
* @return
*/
public boolean canConstruct(String ransomNote, String magazine) {
HashMap<Character, Integer> map1 = new HashMap<>();//ransomNote<字符,出现次数>
HashMap<Character, Integer> map2 = new HashMap<>();
putInHash(ransomNote, map1);
putInHash(magazine, map2);
for (Map.Entry<Character, Integer> entry : map1.entrySet()) {
if (!map2.containsKey(entry.getKey())) {
return false;
}
if (map2.get(entry.getKey()) < entry.getValue()) {
return false;
}
}
return true;
}
public void putInHash(String strings, HashMap<Character, Integer> map) {
for (int i = 0; i < strings.length(); i++) {
if (map.containsKey(strings.charAt(i))) {
int cnt = map.get(strings.charAt(i)) + 1;
map.put(strings.charAt(i), cnt);
} else {
map.put(strings.charAt(i), 1);
}
}
}
}
15.三数之和
代码随想录指路 双指针的思想,以及如何去重------>注意相等之后对right和left的去重并且right--和left++避免内存溢出
class Solution {
/**
* 一个数组里面找到和为0的三元组并且返回三元组
*
* @param nums
* @return
*/
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
//双指针方法---->排序后,left和right去缩范围
Arrays.sort(nums);
for (int i = 0; i < nums.length; i++) {
if (nums[i] > 0) {
break;//跳出循环--->无意义
}
if (i > 0 && nums[i] == nums[i - 1]) {
continue;//去重
}
int left = i + 1, right = nums.length - 1;
while (left < right) {
int sum = nums[i] + nums[left] + nums[right];
if (sum < 0) {
left++;
} else if (sum > 0) {
right--;
} else {
List<Integer> part = new ArrayList<>();
part.add(nums[i]);
part.add(nums[left]);
part.add(nums[right]);
ans.add(part);
//去重,重复的nums[left],nums[right]
while (right > i && nums[right] == nums[right - 1]) {
right--;
}
while (left < right && nums[left] == nums[left + 1]) {
left++;
}
right--;
left++;
}
}
}
return ans;
}
}
*18.四数之和
四元组的和为target即在三数之和的基础上,再加上一层for循环,并且剪枝的条件要相比于三数之和的基础上更为复杂些。
不要判断
nums[k] > target
就返回了,三数之和 可以通过nums[i] > 0
就返回了,因为 0 已经是确定的数了,四数之和这道题目 target是任意值。比如:数组是[-4, -3, -2, -1]
,target
是-10
,不能因为-4 > -10
而跳过。但是我们依旧可以去做剪枝,逻辑变成nums[i] > target && (nums[i] >=0 || target >= 0)
就可以了。
包括j跳过的前提是不能跳过本身拥有的,所以要从i+1之后开始跳过相等
class Solution {
/**
* 四元组的和为target
*
* @param nums
* @param target
* @return
*/
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> ans = new ArrayList<>();
Arrays.sort(nums);
for (int i = 0; i < nums.length; i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
if (nums[i] > target && (nums[i] >= 0 || target >= 0)) {
return ans;
}
for (int j = i + 1; j < nums.length; j++) {
if (j > i+1 && nums[j] == nums[j - 1]) {
continue;
}
int left = j + 1, right = nums.length - 1;
while (left < right) {
int sum = nums[i] + nums[j] + nums[left] + nums[right];
if (sum > target) {
right--;
} else if (sum < target) {
left++;
} else {
ans.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
while (left < right && nums[right] == nums[right - 1]) {
right--;
}
while (left < right && nums[left] == nums[left + 1]) {
left++;
}
right--;
left++;
}
}
}
}
return ans;
}
}
2022.10.03补