偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

你知道嗎?子集問(wèn)題也要去重了!

開(kāi)發(fā) 前端
如果把 子集問(wèn)題、組合問(wèn)題、分割問(wèn)題都抽象為一棵樹(shù)的話,那么組合問(wèn)題和分割問(wèn)題都是收集樹(shù)的葉子節(jié)點(diǎn),而子集問(wèn)題是找樹(shù)的所有節(jié)點(diǎn)!

[[427534]]

子集問(wèn)題+去重

子集II

力扣題目鏈接:https://leetcode-cn.com/problems/subsets-ii/

給定一個(gè)可能包含重復(fù)元素的整數(shù)數(shù)組 nums,返回該數(shù)組所有可能的子集(冪集)。

說(shuō)明:解集不能包含重復(fù)的子集。

示例:

  • 輸入: [1,2,2]
  • 輸出: [ [2], [1], [1,2,2], [2,2], [1,2], [] ]

思路

做本題之前一定要先做78.子集。

這道題目和78.子集區(qū)別就是集合里有重復(fù)元素了,而且求取的子集要去重。

那么關(guān)于回溯算法中的去重問(wèn)題,在40.組合總和II中已經(jīng)詳細(xì)講解過(guò)了,和本題是一個(gè)套路。

劇透一下,后期要講解的排列問(wèn)題里去重也是這個(gè)套路,所以理解“樹(shù)層去重”和“樹(shù)枝去重”非常重要。

用示例中的[1, 2, 2] 來(lái)舉例,如圖所示:(注意去重需要先對(duì)集合排序)

子集II

從圖中可以看出,同一樹(shù)層上重復(fù)取2 就要過(guò)濾掉,同一樹(shù)枝上就可以重復(fù)取2,因?yàn)橥粯?shù)枝上元素的集合才是唯一子集!

本題就是其實(shí)就是78.子集的基礎(chǔ)上加上了去重,去重我們?cè)?0.組合總和II也講過(guò)了,所以我就直接給出代碼了:

C++代碼如下:

  1. class Solution { 
  2. private: 
  3.     vector<vector<int>> result; 
  4.     vector<int> path; 
  5.     void backtracking(vector<int>& nums, int startIndex, vector<bool>& used) { 
  6.         result.push_back(path); 
  7.         for (int i = startIndex; i < nums.size(); i++) { 
  8.             // used[i - 1] == true,說(shuō)明同一樹(shù)支candidates[i - 1]使用過(guò) 
  9.             // used[i - 1] == false,說(shuō)明同一樹(shù)層candidates[i - 1]使用過(guò) 
  10.             // 而我們要對(duì)同一樹(shù)層使用過(guò)的元素進(jìn)行跳過(guò) 
  11.             if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false) { 
  12.                 continue
  13.             } 
  14.             path.push_back(nums[i]); 
  15.             used[i] = true
  16.             backtracking(nums, i + 1, used); 
  17.             used[i] = false
  18.             path.pop_back(); 
  19.         } 
  20.     } 
  21.  
  22. public
  23.     vector<vector<int>> subsetsWithDup(vector<int>& nums) { 
  24.         result.clear(); 
  25.         path.clear(); 
  26.         vector<bool> used(nums.size(), false); 
  27.         sort(nums.begin(), nums.end()); // 去重需要排序 
  28.         backtracking(nums, 0, used); 
  29.         return result; 
  30.     } 
  31. }; 

使用set去重的版本。

  1. class Solution { 
  2. private: 
  3.     vector<vector<int>> result; 
  4.     vector<int> path; 
  5.     void backtracking(vector<int>& nums, int startIndex, vector<bool>& used) { 
  6.         result.push_back(path); 
  7.         unordered_set<int> uset; 
  8.         for (int i = startIndex; i < nums.size(); i++) { 
  9.             if (uset.find(nums[i]) != uset.end()) { 
  10.                 continue
  11.             } 
  12.             uset.insert(nums[i]); 
  13.             path.push_back(nums[i]); 
  14.             backtracking(nums, i + 1, used); 
  15.             path.pop_back(); 
  16.         } 
  17.     } 
  18.  
  19. public
  20.     vector<vector<int>> subsetsWithDup(vector<int>& nums) { 
  21.         result.clear(); 
  22.         path.clear(); 
  23.         vector<bool> used(nums.size(), false); 
  24.         sort(nums.begin(), nums.end()); // 去重需要排序 
  25.         backtracking(nums, 0, used); 
  26.         return result; 
  27.     } 
  28. }; 

補(bǔ)充

本題也可以不適用used數(shù)組來(lái)去重,因?yàn)檫f歸的時(shí)候下一個(gè)startIndex是i+1而不是0。

如果要是全排列的話,每次要從0開(kāi)始遍歷,為了跳過(guò)已入棧的元素,需要使用used。

代碼如下:

  1. class Solution { 
  2. private: 
  3.     vector<vector<int>> result; 
  4.     vector<int> path; 
  5.     void backtracking(vector<int>& nums, int startIndex) { 
  6.         result.push_back(path); 
  7.         for (int i = startIndex; i < nums.size(); i++) { 
  8.             // 而我們要對(duì)同一樹(shù)層使用過(guò)的元素進(jìn)行跳過(guò) 
  9.             if (i > startIndex && nums[i] == nums[i - 1] ) { // 注意這里使用i > startIndex 
  10.                 continue
  11.             } 
  12.             path.push_back(nums[i]); 
  13.             backtracking(nums, i + 1); 
  14.             path.pop_back(); 
  15.         } 
  16.     } 
  17.  
  18. public
  19.     vector<vector<int>> subsetsWithDup(vector<int>& nums) { 
  20.         result.clear(); 
  21.         path.clear(); 
  22.         sort(nums.begin(), nums.end()); // 去重需要排序 
  23.         backtracking(nums, 0); 
  24.         return result; 
  25.     } 
  26. }; 

總結(jié)

其實(shí)這道題目的知識(shí)點(diǎn),我們之前都講過(guò)了,如果之前講過(guò)的子集問(wèn)題和去重問(wèn)題都掌握的好,這道題目應(yīng)該分分鐘AC。

當(dāng)然本題去重的邏輯,也可以這么寫(xiě)

  1. if (i > startIndex && nums[i] == nums[i - 1] ) { 
  2.         continue

其他語(yǔ)言版本

Java

  1. class Solution { 
  2.    List<List<Integer>> result = new ArrayList<>();// 存放符合條件結(jié)果的集合 
  3.    LinkedList<Integer> path = new LinkedList<>();// 用來(lái)存放符合條件結(jié)果 
  4.    boolean[] used; 
  5.     public List<List<Integer>> subsetsWithDup(int[] nums) { 
  6.         if (nums.length == 0){ 
  7.             result.add(path); 
  8.             return result; 
  9.         } 
  10.         Arrays.sort(nums); 
  11.         used = new boolean[nums.length]; 
  12.         subsetsWithDupHelper(nums, 0); 
  13.         return result; 
  14.     } 
  15.  
  16.     private void subsetsWithDupHelper(int[] nums, int startIndex){ 
  17.         result.add(new ArrayList<>(path)); 
  18.         if (startIndex >= nums.length){ 
  19.             return
  20.         } 
  21.         for (int i = startIndex; i < nums.length; i++){ 
  22.             if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1]){ 
  23.                 continue
  24.             } 
  25.             path.add(nums[i]); 
  26.             used[i] = true
  27.             subsetsWithDupHelper(nums, i + 1); 
  28.             path.removeLast(); 
  29.             used[i] = false
  30.         } 
  31.     } 

Python

  1. class Solution: 
  2.     def subsetsWithDup(self, nums: List[int]) -> List[List[int]]: 
  3.         res = []  #存放符合條件結(jié)果的集合 
  4.         path = []  #用來(lái)存放符合條件結(jié)果 
  5.         def backtrack(nums,startIndex): 
  6.             res.append(path[:]) 
  7.             for i in range(startIndex,len(nums)): 
  8.                 if i > startIndex and nums[i] == nums[i - 1]:  #我們要對(duì)同一樹(shù)層使用過(guò)的元素進(jìn)行跳過(guò) 
  9.                     continue 
  10.                 path.append(nums[i]) 
  11.                 backtrack(nums,i+1)  #遞歸 
  12.                 path.pop()  #回溯 
  13.         nums = sorted(nums)  #去重需要排序 
  14.         backtrack(nums,0) 
  15.         return res 

Go

  1. var res[][]int 
  2. func subsetsWithDup(nums []int)[][]int { 
  3.  res=make([][]int,0) 
  4.   sort.Ints(nums) 
  5.  dfs([]int{},nums,0) 
  6.  return res 
  7. func dfs(temp, num []int, start int)  { 
  8.  tmp:=make([]int,len(temp)) 
  9.  copy(tmp,temp
  10.  
  11.  res=append(res,tmp) 
  12.  for i:=start;i<len(num);i++{ 
  13.   if i>start&&num[i]==num[i-1]{ 
  14.    continue 
  15.   } 
  16.   temp=append(temp,num[i]) 
  17.   dfs(temp,num,i+1) 
  18.   temp=temp[:len(temp)-1] 
  19.  } 

Javascript

  1. var subsetsWithDup = function(nums) { 
  2.     let result = [] 
  3.     let path = [] 
  4.     let sortNums = nums.sort((a, b) => { 
  5.         return a - b 
  6.     }) 
  7.     function backtracing(startIndex, sortNums) { 
  8.         result.push(path.slice(0)) 
  9.         if(startIndex > nums.length - 1) { 
  10.             return 
  11.         } 
  12.         for(let i = startIndex; i < nums.length; i++) { 
  13.             if(i > startIndex && nums[i] === nums[i - 1]) { 
  14.                 continue 
  15.             } 
  16.             path.push(nums[i]) 
  17.             backtracing(i + 1, sortNums) 
  18.             path.pop() 
  19.         } 
  20.     } 
  21.     backtracing(0, sortNums) 
  22.     return result 
  23. }; 

 

責(zé)任編輯:姜華 來(lái)源: 代碼隨想錄
相關(guān)推薦

2021-10-12 08:43:20

排列回溯算法

2021-09-29 11:30:03

子集問(wèn)題模板題

2022-12-02 14:12:52

新能源汽車(chē)海爾

2017-01-18 18:28:54

大數(shù)據(jù)數(shù)據(jù)庫(kù)技術(shù)

2024-04-07 00:00:03

2025-01-20 00:00:00

Java語(yǔ)言Record

2019-06-06 15:00:25

手機(jī)iPhone摩托羅拉

2024-10-12 08:01:53

2018-12-27 08:50:06

JavaScript開(kāi)源

2023-12-20 08:23:53

NIO組件非阻塞

2024-04-30 09:02:48

2023-04-26 10:21:04

2023-12-12 08:41:01

2024-05-28 09:12:10

2024-04-07 00:00:00

ESlint命令變量

2024-07-17 08:12:06

2016-03-18 19:03:35

認(rèn)知計(jì)算IBM

2024-01-09 07:29:05

Argo代碼庫(kù)應(yīng)用程序

2019-12-12 09:23:29

Hello World操作系統(tǒng)函數(shù)庫(kù)

2017-10-16 13:45:04

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)