454. 四数相加 II
题目
给定四个包含整数的数组 A, B, C, D,计算有多少个元组 (i, j, k, l) 使得 A[i] + B[j] + C[k] + D[l] = 0。
解题思路
- 先计算数组
A和B的所有组合和,并存入哈希表map中,键为组合和,值为该和出现的次数。 - 然后遍历数组
C和D的所有组合,查找-(C[k] + D[l])是否存在于map中,如果存在则累加结果。
反思
使用哈希表可以有效地降低时间复杂度,从 O(n^4) 降到 O(n^2),提高了效率。
代码
class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
int res = 0;
Map<Integer, Integer> map = new HashMap<>();
for(int i : nums1) {
for(int j : nums2) {
int sum = i + j;
map.put(sum, map.getOrDefault(sum, 0) + 1);
}
}
for(int i : nums3) {
for(int j : nums4) {
res += map.getOrDefault(0 - i - j, 0);
}
}
return res;
}
}
383. 赎金信
题目
给定一个赎金信字符串和一个杂志字符串,判断赎金信能否由杂志中的字符构成。每个字符只能使用一次。
解题思路
- 创建一个长度为 26 的数组,用于记录每个字母在杂志中出现的次数。
- 遍历赎金信,检查数组中对应字母的计数,如果不足则返回
false,否则减一。
反思
直接使用数组存储字符频次比使用哈希表更简单,并且减少了空间占用。
代码
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
if(ransomNote.length() > magazine.length()) {
return false;
}
int[] nums = new int[26];
for(char i : magazine.toCharArray()) {
nums[i - 'a'] += 1;
}
for(char i : ransomNote.toCharArray()) {
if(nums[i - 'a'] == 0) {
return false;
} else {
nums[i - 'a'] -= 1;
}
}
return true;
}
}
15. 三数之和
题目
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a, b, c 使得 a + b + c = 0,找出所有满足条件且不重复的三元组。
解题思路
- 先对数组排序,然后使用双指针法。
- 遍历每个元素
a,固定a后,使用左右指针寻找b和c使得a + b + c = 0。 - 为避免重复结果,需要跳过重复元素。
反思
使用 while 循环去重是必须的,尤其在左右指针移动时对比的数要注意防止重复。
代码
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, right = nums.length - 1;
while (left < right) {
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 (left < right && nums[left] == nums[left + 1]) left++;
while (left < right && nums[right] == nums[right - 1]) right--;
left++;
right--;
}
}
}
return result;
}
}
18. 四数之和
题目
给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a, b, c, d 使得 a + b + c + d = target,找出所有满足条件且不重复的四元组。
解题思路
- 对数组进行排序,然后使用两层循环固定前两个元素,再使用双指针寻找后两个元素。
- 注意跳过重复元素,防止结果重复。
反思
if 语句用于去重时比 while 更合适,因为只需要一次性跳过重复的前一个元素。
代码
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 - 3; i++) {
if (nums[i] > target && nums[i] >= 0) {
break;
}
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
for (int j = i + 1; j < nums.length - 2; j++) {
if (nums[i] + nums[j] > target && nums[i] + nums[j] >= 0) {
break;
}
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 {
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;
}
}

被折叠的 条评论
为什么被折叠?



