454. 四数相加 II
解题思路:
1.定义一个map,key:a+b的数值,value:a+b数值出现的次数。
2.遍历nums1和nums2数组,统计两个数组元素之和,和出现的次数,放到map中。
3.之后遍历nums3和nums4数组,找到如果 0-(c+d)在map中出现过的话,就把map中key对应的value也就是出现次数统计出来。
class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
//key:a+b的数值,value:a+b数值出现的次数
HashMap<Integer, Integer> map = new HashMap<>();
// 统计a+b+c+d = 0 出现的次数
int res = 0;
int temp = 0;
// 遍历nums1和nums2数组,统计两个数组元素之和,和出现的次数,放到map中
for (int i : nums1) {
for (int j : nums2) {
temp = i + j;
if (map.containsKey(temp)) {
map.put(temp, map.get(temp) + 1);
} else {
map.put(temp, 1);
}
}
}
// 在遍历nums3和nums4数组,找到如果 0-(c+d)在map中出现过的话,
// 就把map中key对应的value也就是出现次数统计出来。
for (int k : nums3) {
for (int h : nums4) {
temp = 0 - (k + h);
if (map.containsKey(temp)) {
res += map.get(temp);
}
}
}
return res;
}
}
383.赎金信
解题思路:
1.定义一个长度为26的数组record。
2. 通过record数据记录 magazine里各个字符出现次数。
3.遍历ransomNote,在record里对应的字符个数做--操作。
4.遍历record,如果有元素小于零说明ransomNote里出现的字符,magazine没有。即ransomNote的字符更多,magazine中的字符不够。
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] record = new int[26];
for (int i = 0; i < magazine.length(); i++) {
// 通过record数据记录 magazine里各个字符出现次数
record[magazine.charAt(i) - 'a']++;
}
for (int i = 0; i < ransomNote.length(); i++) {
// 遍历ransomNote,在record里对应的字符个数做--操作
record[ransomNote.charAt(i) - 'a']--;
}
for (int k : record) {
// 如果小于零说明ransomNote里出现的字符,magazine没有
// 即ransomNote的字符更多,magazine中的字符不够
if (k < 0) {
return false;
}
}
return true;
}
}
15.三数之和
解题思路:
1.将原数组nums排序。
2. 双指针:
- left 定义在i+1的位置上,right 在数组结尾的位置上。
- 在数组中找到 abc 使得a + b +c =0,相当于 a = nums[i],b = nums[left],c = nums[right]。
- nums[i] + nums[left] + nums[right] > 0 ,此时三数之和大了,因为数组是排序后了,所以right下标就应该向左移动,让三数之和小一些。
- 如果 nums[i] + nums[left] + nums[right] < 0 说明 此时 三数之和小了,left 就向右移动,才能让三数之和大一些,直到left与right相遇为止。
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if (nums == null) {
return res;
}
//将原数组排序
Arrays.sort(nums);
for (int i = 0; i < nums.length; i++) {
//排序后,当前数大于0,后边所有数都大于0,不存在a+b+c=0
if (nums[i] > 0) {
return res;
}
//去重
//不能写成 nums[i] == nums[i + 1],会漏解
//例如{-1, -1 ,2} 这组数据,当遍历到第一个-1 的时候,判断 下一个也是-1,那这组数据就pass了。
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
//双指针
int left = i + 1;
int right = nums.length - 1;
while (left < right) {
// 去重复逻辑如果放在这里,0,0,0 的情况,可能直接导致 right<=left 了,
// 从而漏掉了 0,0,0 这种三元组
/*
while (right > left && nums[right] == nums[right - 1]) right--;
while (right > left && nums[left] == nums[left + 1]) left++;
*/
int sum = nums[i] + nums[left] + nums[right];
if (sum > 0) {
//nums[i] + nums[left] + nums[right] > 0
// 说明 此时 三数之和大了,right就向左移动
right--;
} else if (sum < 0) {
//nums[i] + nums[left] + nums[right] < 0
// 说明 此时 三数之和小了,left就向右移动
left++;
} else {
//收集结果
res.add(Arrays.asList(nums[i], nums[left], nums[right]));
//去重
while (left < right && nums[left] == nums[left + 1]) {
left++;
}
while (left < right && nums[right] == nums[right - 1]) {
right--;
}
//这里left和right要同时移动,因为nums[i]确定,若left不动,nums[left]就固定了
//此时,三个数两个数固定,移动right就算满足条件也是重复解
right--;
left++;
}
}
}
return res;
}
}
18.四数之和
解题思路:
整体逻辑和三数之和类似,就是多套了层循环。套循环后,需要额外注意第二层循环的起始位置为i+1,相应的判断也要从i+1开始。
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> res = new ArrayList<>();
if (nums == null) {
return res;
}
//将原数组排序
Arrays.sort(nums);
for (int i = 0; i < nums.length; i++) {
//1.不要判断nums[k] > target 就返回了,
//2.三数之和 可以通过 nums[i] > 0 就返回了,因为 0 已经是确定的数了,
//3.四数之和这道题目 target是任意值。
//4.比如:数组是[-4, -3, -2, -1],target是-10,不能因为-4 > -10而跳过。
//5.但只要num[i]为正数或者target为正数就好了,这样大于一个正数就可以剪枝了。
if (nums[i] > target && (target > 0 || nums[i] > 0)) {
return res;
}
//去重
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
for (int j = i + 1; j < nums.length; j++) {
//这里不判断的话会出错,leetcode给的例子为[2,2,2,2],target = 8
//错误结果为[[2,2,2,2],[2,2,2,2]],正确结果为[[2,2,2,2]]
if (j > i + 1 && nums[j] == nums[j - 1]) {
continue;
}
//双指针
int left = j + 1;
int right = nums.length - 1;
while (left < right) {
int sum = nums[i] + nums[j] + nums[left] + nums[right];
if (sum > target) {
// nums[i] + num[j] + nums[left] + nums[right] > target
// 说明此时四数之和大了,right就向左移动
right--;
} else if (sum < target) {
// nums[i] + num[j] + nums[left] + nums[right] < target
// 说明此时四数之和小了,left就向右移动
left++;
} else {
//收集结果
res.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
//去重
while (left < right && nums[left] == nums[left + 1]) {
left++;
}
while (left < right && nums[right] == nums[right - 1]) {
right--;
}
//这里left和right要同时移动,因为nums[i]确定,若left不动,nums[left]就固定了
//此时,三个数两个数固定,移动right就算满足条件也是重复解
right--;
left++;
}
}
}
}
return res;
}
}