454. 四数相加II
题目链接:454. 四数相加 II - 力扣(LeetCode)
把四个数组拆分成两组来做,先遍历A和B,把A和B的和记录在map中,键值是sum,值是这个sum出现的次数,之后遍历C和D,判断map中是否存在 target - (C + D),取出value,加在最终的答案result 上。
代码的时间复杂度有点高,我觉得是遍历了所有四个数组的原因。
class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
HashMap<Integer, Integer> sumResult = new HashMap<>();
for (int i = 0; i < nums1.length; i++) {
for (int j = 0; j < nums2.length; j++) {
int sum = nums1[i] + nums2[j];
if (sumResult.containsKey(sum)) {
int value = sumResult.get(sum);
sumResult.put(sum, value + 1);
} else {
sumResult.put(sum, 1);
}
}
}
int count = 0;
for (int i = 0; i < nums3.length; i++) {
for (int j = 0; j < nums4.length; j++) {
int minus = 0 - nums3[i] - nums4[j];
if (!sumResult.containsKey(minus)) {
continue;
} else {
count += sumResult.get(minus);
}
}
}
return count;
}
}
383. 赎金信
没什么特殊的,用数组作为哈希表,先遍历magazine记录字符出现的次数,然后遍历ransomNote,对每一个索引上的值减1,如果减不了就返回false。
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] set = new int[26];
for (int i = 0; i < magazine.length(); i++) {
char c = magazine.charAt(i);
set[c - 'a']++;
}
for (int i = 0; i < ransomNote.length(); i++) {
char c = ransomNote.charAt(i);
if (set[c - 'a'] == 0)
return false;
set[c - 'a']--;
}
return true;
}
}
15. 三数之和
这道题记得关键点:
首先对数组进行升序排列,分别用三个指针,指向数组中的三个数,使三个数的和为0,寻找这三个数的过程中,将三数之和与零进行对比,若小于0,就将最右边的指针向右移动,若大于0,就将左边的指针向左移动。
但是具体在写的时候,有许多细节需要考虑,比如:题目要求对结果进行去重、排序方法。
首先排序直接使用sort方法。如何定义三个指针的初始位置,以及如何分别移动三个指针?
写着写着就变成三层for循环了hhhhhh
还是直接看代码随想录吧:(动画来自代码随想录)
三个指针的起始位置分别是:for循环i从起始位置0开始遍历,left初始在i+1的位置,right放在末尾length-1。
移动的时候有一个很重要的点:去重问题!
如果用我现在的去重方法(在移动left和right时,判断left和left+1的元素是否相等,以及判断right和right-1的元素是否相等,相等就继续移动),但是这样的方法,可能会直接忽略掉【-1,-1,2】这种结果。因此去重的逻辑是:对i进行去重!这样是不会忽略掉上面这种情况的,对right和left 的去重可以依旧使用上面两种情况。
双指针法将时间复杂度:O(n^2)的解法优化为 O(n)的解法。也就是降一个数量级
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(nums);
int len = nums.length;
int left = 0;
int right = 0;
if (nums[0] > 0 || nums[len - 1] < 0)
return result;
int sum = 0;
for (int i = 0; i < len - 2; i++) {
if (i > 0 && nums[i] == nums[i - 1])
continue;
left = i + 1;
right = len - 1;
while (left < right) {
sum = nums[i] + nums[left] + nums[right];
if (sum < 0) {
left++;
} else if (sum > 0) {
right--;
} else if (sum == 0) {
List<Integer> list = new ArrayList<>();
list.add(nums[i]);
list.add(nums[left]);
list.add(nums[right]);
result.add(list);
while (left < right && nums[left] == nums[left + 1]) {
left++;
}
while (left < right && nums[right] == nums[right - 1]) {
right--;
}
left++;
right--;
}
}
}
return result;
}
}
18. 四数之和:
相比于上一题就是多套了一层循环。
但是这里有个坑!
如果不加那句剪枝操作,会越界!!!然后出现一个神奇的事情:
测试样例第292个的数据是:
如果不剪枝,竟然sum的结果就是-294967296,不debug一下都发现不了这个问题。。。
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(nums);
int len = nums.length;
int sum = 0;
for (int i = 0; i < len - 3; i++) {
if (nums[i] > 0 && nums[i] > target) {
return result;
}
if (i > 0 && nums[i] == nums[i - 1])
continue;
for (int j = i + 1; j < len - 2; j++) {
if (j > i + 1 && nums[j] == nums[j - 1])
continue;
int left = j + 1;
int right = len - 1;
while (left < right) {
sum = nums[i] + nums[j] + nums[left] + nums[right];
if (sum > target) {
right--;
} else if (sum < target) {
left++;
} else if (sum == target) {
result.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--;
}
}
}
}
return result;
}
}
day 7 完结撒花~