题目:
- 四数相加Ⅱ,209.长度最小的子数组,59.螺旋矩阵II
学习内容:
454.四数相加Ⅱ
使用hashmap,key存储两项的sum,value存储sum出现的次数。使用两个双循环解决。
class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
Map<Integer,Integer> hashmap = new HashMap<>(); // 定义map,key为两项相加和,value为结果出现次数
int res = 0;
for (int i : nums1) {
for (int j : nums2) {
int sum = i + j;
hashmap.put(sum, hashmap.getOrDefault(sum,0) + 1);
}
}
for (int i : nums3) {
for (int j : nums4) {
int sum = i + j;
res += hashmap.getOrDefault(-sum, 0);
}
}
return res;
}
}
时间复杂度:O(
n
2
n^2
n2)
空间复杂度:O(
n
2
n^2
n2)
383. 赎金信
map法比较好想,但是感觉有点难写,要调用多个函数。
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
Map<Character,Integer> hashmap = new HashMap<>(); // key为char,value为出现次数
char[] m = magazine.toCharArray();
char[] r = ransomNote.toCharArray();
for (char c : m) {
hashmap.put(c, hashmap.getOrDefault(c, 0) + 1);
}
for (char c : r) {
if (hashmap.containsKey(c) && hashmap.get(c) != 0) {
hashmap.put(c, hashmap.get(c) - 1);
} else {
return false;
}
}
return true;
}
}
时间复杂度:O(n)
空间复杂度:O(1)
15.三数之和
核心:1.必须先排序 2.双指针 3.去重逻辑,先去重a,再去重bc。
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; // 去重a
int left = i + 1;
int right = nums.length - 1;
while (right > left) {
int sum = nums[i] + nums[left] + nums[right]; // 计算sum
if (sum > 0) {
right--; // 太大了,right指针往小的移动
} else if (sum < 0) {
left++; // 太小了,left指针往大的移动
} else {
result.add(Arrays.asList(nums[i], nums[left], nums[right])); // 添加结果
// bc去重
while (right > left && nums[left] == nums[left + 1]) left++; // 现在left的如果和右一个相等,跳过
while (right > left && nums[right] == nums[right - 1]) right--; // 现在right的如果和左一个相等,跳过
left++;
right--;
}
}
}
return result;
}
}
18.四数之和
三数之和的延展。
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++) {
if (nums[i] > 0 && nums[i] > target) return result;
if (i > 0 && nums[i - 1] == nums[i]) continue; // 对nums[i]去重
for (int j = i + 1; j < nums.length; j++) {
if (j > i + 1 && nums[j] == nums[j - 1]) continue; // 对nums[j]去重
int left = j + 1;
int right = nums.length - 1;
while (right > left) {
int sum = 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[left] == nums[left + 1]) left++;
while (right > left && nums[right] == nums[right - 1]) right--;
left++;
right--;
}
}
}
}
return result;
}
}
总结
- hashmap常见的写法
hashmap.put(a, hashmap.getOrDefault(a, 0) + 1);
表示放置key a到hashmap中,如果已经存在,则++,不存在则从0开始。 hashmap.containsKey(a);
判断是否有key a在hashmap中。- 将String a转为char数组
a.toCharArray();
- 创建包含指定元素的list:
Arrays.asList(a, b, c);
学习时间:
2024.3.12