wordpress主頁居中重慶seo俱樂部
454. 四數(shù)相加 II
題目
給定四個包含整數(shù)的數(shù)組 A
, B
, C
, D
,計算有多少個元組 (i, j, k, l)
使得 A[i] + B[j] + C[k] + D[l] = 0
。
解題思路
- 先計算數(shù)組
A
和B
的所有組合和,并存入哈希表map
中,鍵為組合和,值為該和出現(xiàn)的次數(shù)。 - 然后遍歷數(shù)組
C
和D
的所有組合,查找-(C[k] + D[l])
是否存在于map
中,如果存在則累加結(jié)果。
反思
使用哈希表可以有效地降低時間復雜度,從 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. 贖金信
題目
給定一個贖金信字符串和一個雜志字符串,判斷贖金信能否由雜志中的字符構成。每個字符只能使用一次。
解題思路
- 創(chuàng)建一個長度為 26 的數(shù)組,用于記錄每個字母在雜志中出現(xiàn)的次數(shù)。
- 遍歷贖金信,檢查數(shù)組中對應字母的計數(shù),如果不足則返回
false
,否則減一。
反思
直接使用數(shù)組存儲字符頻次比使用哈希表更簡單,并且減少了空間占用。
代碼
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. 三數(shù)之和
題目
給定一個包含 n
個整數(shù)的數(shù)組 nums
,判斷 nums
中是否存在三個元素 a, b, c
使得 a + b + c = 0
,找出所有滿足條件且不重復的三元組。
解題思路
- 先對數(shù)組排序,然后使用雙指針法。
- 遍歷每個元素
a
,固定a
后,使用左右指針尋找b
和c
使得a + b + c = 0
。 - 為避免重復結(jié)果,需要跳過重復元素。
反思
使用 while
循環(huán)去重是必須的,尤其在左右指針移動時對比的數(shù)要注意防止重復。
代碼
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. 四數(shù)之和
題目
給定一個包含 n
個整數(shù)的數(shù)組 nums
和一個目標值 target
,判斷 nums
中是否存在四個元素 a, b, c, d
使得 a + b + c + d = target
,找出所有滿足條件且不重復的四元組。
解題思路
- 對數(shù)組進行排序,然后使用兩層循環(huán)固定前兩個元素,再使用雙指針尋找后兩個元素。
- 注意跳過重復元素,防止結(jié)果重復。
反思
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;}
}