个人博客主页:http://myblog.nxx.nx.cn
 代码GitHub地址:https://github.com/nx-xn2002/Data_Structure.git
Day7
454. 四数相加 II
题目链接:
 https://leetcode.cn/problems/4sum-ii/
题目描述:
 给你四个整数数组 nums1、nums2、nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足:
- 0 <= i, j, k, l < n
- nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0
思路:
 看到四数相加,第一反应是双指针的题目,然后才发现这题和之前的双指针不一样。双指针那道题是单个数组中找四个元素,而这道题是从分别从 4 个数组中各取一个元素。因此我们可以用前一天的两数相加的思路来做这一题,将四个数组两两组合相加,并存储结果。
代码实现:
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {int count = 0;HashMap<Integer, Integer> map = new HashMap<>();for (int n1 : nums1) {for (int n2 : nums2) {map.put(n1 + n2, map.getOrDefault(n1 + n2, 0) + 1);}}for (int n3 : nums3) {for (int n4 : nums4) {count += map.getOrDefault(-n3 - n4, 0);}}return count;
}
- 时间复杂度:O(N ^ 2)
- 空间复杂度:O(N ^ 2)
383. 赎金信
题目链接:
 https://leetcode.cn/problems/ransom-note/
题目描述:
 给你两个字符串:ransomNote 和 magazine ,判断 ransomNote 能不能由 magazine 里面的字符构成。
如果可以,返回 true ;否则返回 false 。
magazine 中的每个字符只能在 ransomNote 中使用一次。
思路:
 这题的思路和之前的字母异位词基本相同,只不过限制条件没有那么苛刻,只需要 ransomNote 可以由 magazine 中出现的字符拼成就行了,那就只需要先把 magazine 中的字符出现次数存储到一个哈希表中,然后遍历 ransomNote 数组的时候把哈希表中对应字符次数减一,如果字符次数小于 0,就说明无法构成。
代码实现:
public boolean canConstruct(String ransomNote, String magazine) {if (magazine.length() < ransomNote.length()) {return false;}int[] map = new int[26];for (int i = 0; i < magazine.length(); i++) {map[magazine.charAt(i) - 'a']++;}for (int i = 0; i < ransomNote.length(); i++) {int pos = ransomNote.charAt(i) - 'a';if (map[pos]-- <= 0) {return false;}}return true;
} 
- 时间复杂度:O(M + N)
- 空间复杂度:O(M + N)
15. 三数之和
题目链接:
 https://leetcode.cn/problems/3sum/
题目描述:
 给你一个整数数组 nums ,判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i != j、i != k 且 j != k ,同时还满足 nums[i] + nums[j] + nums[k] == 0 。请
你返回所有和为 0 且不重复的三元组。
注意:答案中不可以包含重复的三元组。
思路:
 这题还是用双指针思路比较简单,也方便进行剪枝。要使用双指针,我们需要先将数组进行排序。然后先嵌套两个 for 循环来获取三元组中的前两个数。因为我的数组是从小到大排序的,如果第一个数大于 0,或者前两个数相加大于 0,就说明后面不可能找到与前两个数相加等于 0 的数了,可以直接返回结果。反之,可以从第二个数的位置到数组最后一位之间使用二分法开始查找第三个数。
代码实现:
/*** 双指针*/
public List<List<Integer>> threeSum(int[] nums) {int n = nums.length;Arrays.sort(nums);List<List<Integer>> ans = new ArrayList<List<Integer>>();for (int first = 0; first < n; ++first) {if (first > 0 && nums[first] == nums[first - 1]) {continue;}int third = n - 1;int target = -nums[first];for (int second = first + 1; second < n; ++second) {if (second > first + 1 && nums[second] == nums[second - 1]) {continue;}while (second < third && nums[second] + nums[third] > target) {--third;}if (second == third) {break;}if (nums[second] + nums[third] == target) {ans.add(List.of(nums[first], nums[second], nums[third]));}}}return ans;
}
18. 四数之和
题目链接:
 https://leetcode.cn/problems/4sum/
题目描述:
 给你一个整数数组 nums ,判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i != j、i != k 且 j != k ,同时还满足 nums[i] + nums[j] + nums[k] == 0 。请
你返回所有和为 0 且不重复的三元组。
注意:答案中不可以包含重复的三元组。
思路:
 这题思路与上一题基本一致,不过就是二分法的时候,是用两端指针的值进行判断,而不是 mid 指针指向的值进行判断。
代码实现:
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;}for (int j = i + 1; j < nums.length; j++) {if (j > i + 1 && nums[j - 1] == nums[j]) {continue;}int left = j + 1;int right = nums.length - 1;while (right > left) {long sum = (long) 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[right] == nums[right - 1]) {right--;}while (right > left && nums[left] == nums[left + 1]) {left++;}left++;right--;}}}}return result;
}