LeetCode:兩數之和、三數之和、四數之和
- 2020 年 3 月 7 日
- 筆記
LeetCode:兩數之和、三數之和、四數之和
多數之和問題,利用哈希集合減少時間複雜度以及多指針收縮窗口的巧妙解法
No.1 兩數之和
給定一個整數數組 nums 和一個目標值 target,請你在該數組中找出和為目標值的那 兩個 整數,並返回他們的數組下標。
你可以假設每種輸入只會對應一個答案。但是,你不能重複利用這個數組中同樣的元素
示例:
給定 nums = [2, 7, 11, 15], target = 9 因為 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1]
-
窮舉所有組合
時間複雜度:(O(n^2)),兩次循環
空間複雜度:(O(1))
兩層循環,判斷每一種組合
-
利用哈希減少時間複雜度
時間複雜度:(O(n)),一層循環(O(n)) * 哈希查找(O(1))
空間複雜度:(O(n)),哈希表最多存 n 個元素
將數組元素存入哈希表,查找對應目標元素是否在數組中
對於每個元素,查找後面是否有目標元素
public int[] twoSum(int[] nums, int target) { Map<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < nums.length; i++) map.put(nums[i], i); for (int i = 0; i < nums.length; i++) { int j = target - nums[i]; if (map.containsKey(j) && map.get(j) != i) { return new int[] { i, map.get(j) }; } } return new int[]{}; }
優化,用一次循環解決問題。對於每個元素,查找前面是否有目標元素
public int[] twoSum(int[] nums, int target) { Map<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { int j = target - nums[i]; if (map.containsKey(j) && map.get(j) != i) { return new int[]{i, map.get(j)}; } map.put(nums[i], i); } return new int[]{}; }
Map 存入相同元素會被覆蓋,本題中並不影響結果
No.15 三數之和
給定一個包含 n 個整數的數組 nums,判斷 nums 中是否存在三個元素 a,b,c ,使得 a + b + c = 0 ?找出所有滿足條件且不重複的三元組
注意:答案中不可以包含重複的三元組
例如, 給定數組 nums = [-1, 0, 1, 2, -1, -4] 滿足要求的三元組集合為: [ [-1, 0, 1], [-1, -1, 2] ]
-
窮舉所有組合
時間複雜度:(O(n^3)),三重循環(O(n^3))
空間複雜度:(O(1))
三層循環,判斷所有組合,去重
-
利用哈希表減少時間複雜度
時間複雜度:(O(n^2)),排序 (O(nlogn)) + 兩重循環 (O(n^2)) * 哈希查找 (O(1))
空間複雜度:(O(n))
先進行排序,有利於後面去重
兩層循環,判斷目標值是否在數組中,並且保證在後方數據中查找,以免造成重複
public List<List<Integer>> threeSum(int[] nums) { List<List<Integer>> lists = new ArrayList<>(); if (nums.length < 3) return lists; Arrays.sort(nums);//排序 HashMap<Integer, Integer> map = new HashMap<>(); for (int i=0;i<nums.length;i++) map.put(nums[i],i); for (int i = 0; i < nums.length - 2; i++) { if (i > 0 && nums[i] == nums[i - 1]) continue;//去重 for (int j = i + 1; j < nums.length; j++) { if (j > i + 1 && nums[j] == nums[j - 1]) continue;//去重 int a = nums[i]; int b = nums[j]; int c = -a - b; if (map.containsKey(c) && map.get(c) > j) {//向後查找 lists.add(new ArrayList<Integer>() {{ add(a); add(b); add(c); }}); } else continue; } } return lists; }
-
利用數據特點,雙指針收縮窗口
時間複雜度:(O(n^2))
空間複雜度:(O(1))
先進行排序,有利於去重
三元組 [ k,i,j ],每一個 k,i = k+1,j = length-1
求和,大於 0 ,j 右移,小於 0,i 左移,等於 0,存入 List
public List<List<Integer>> threeSum(int[] nums) { List<List<Integer>> lists = new ArrayList<>(); if (nums.length < 3) return lists; Arrays.sort(nums);//排序 for (int k = 0; k < nums.length - 2; k++) { if (k > 0 && nums[k] == nums[k - 1]) continue;//去重 int i = k + 1, j = nums.length - 1; while (i < j) { int sum = nums[k] + nums[i] + nums[j]; if (sum < 0) i++; else if (sum > 0) j--; else { List<Integer> list = new ArrayList<>(); list.add(nums[k]); list.add(nums[i]); list.add(nums[j]); lists.add(list); while (i < j && nums[i] == nums[i + 1]) i++;//去重 while (i < j && nums[j] == nums[j - 1]) j--;//去重 i++; j--; } } } return lists; }
No.18 四數之和
給定一個包含 n 個整數的數組 nums 和一個目標值 target,判斷 nums 中是否存在四個元素 a,b,c 和 d ,使得 a + b + c + d 的值與 target 相等?找出所有滿足條件且不重複的四元組
注意:答案中不可以包含重複的四元組
示例:
給定數組 nums = [1, 0, -1, 0, -2, 2],和 target = 0 滿足要求的四元組集合為: [ [-1, 0, 0, 1], [-2, -1, 1, 2], [-2, 0, 0, 2] ]
-
利用哈希表減少時間複雜度
時間複雜度:(O(n^3)),兩重循環 (O(n^3)) * 哈希查找 (O(1))
空間複雜度:(O(n))
public List<List<Integer>> fourSum(int[] nums, int target) { List<List<Integer>> lists = new ArrayList<>(); if (nums.length < 4) return lists; Arrays.sort(nums); HashMap<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < nums.length; i++) map.put(nums[i], i); for (int i = 0; i < nums.length - 2; i++) { if (i > 0 && nums[i] == nums[i - 1]) continue;//去重 for (int j = i + 1; j < nums.length - 1; j++) { if (j > i + 1 && nums[j] == nums[j - 1]) continue;//去重 for (int k = j + 1; k < nums.length; k++) { if (k > j + 1 && nums[k] == nums[k - 1]) continue;//去重 int a = nums[i]; int b = nums[j]; int c = nums[k]; int d = target - a - b - c; if (map.containsKey(d) && map.get(d) > k) {//向後查找 lists.add(new ArrayList<Integer>() {{ add(a); add(b); add(c); add(d); }}); } else continue; } } } return lists; }
-
利用數據特點,雙指針收縮窗口
時間複雜度:(O(n^3)),三重循環(O(n^3))
空間複雜度:(O(n))
public List<List<Integer>> fourSum(int[] nums, int target) { List<List<Integer>> lists = new ArrayList<>(); if (nums.length < 4) return lists; Arrays.sort(nums); for (int a = 0; a < nums.length - 3; a++) { if (a > 0 && nums[a] == nums[a - 1]) continue;//去重 for (int b = a + 1; b < nums.length - 2; b++) { if (b > a + 1 && nums[b] == nums[b - 1]) continue;//去重 int c = b + 1, d = nums.length - 1; while (c < d) { int sum = nums[a] + nums[b] + nums[c] + nums[d]; if (sum < target) c++; else if (sum > target) d--; else { List<Integer> list = new ArrayList<>(); list.add(nums[a]); list.add(nums[b]); list.add(nums[c]); list.add(nums[d]); lists.add(list); while (c < d && nums[c] == nums[c + 1]) c++;//去重 while (c < d && nums[d] == nums[d - 1]) d--;//去重 c++; d--; } } } } return lists; }