【Lintcode】018.Subsets II

題目:

Given a list of numbers that may has duplicate numbers, return all possible subsets

Notice

  • Each element in a subset must be in?non-descending?order.
  • The ordering between two subsets is free.
  • The solution set must not contain duplicate subsets.

Example

If S =?[1,2,2], a solution is:

[[2],[1],[1,2,2],[2,2],[1,2],[]
]

題解:

Solution 1 ()

class Solution {
public:vector<vector<int> > subsetsWithDup(vector<int> S) {vector<vector<int> > res;vector<int> v;sort(S.begin(), S.end());Dfs(S, res, v, 0);return res;}void Dfs(vector<int> S, vector<vector<int> > &res, vector<int> &v, int pos) {res.push_back(v);for (int i = pos; i < S.size(); ++i) {if (i == pos || S[i] != S[i - 1]) {v.push_back(S[i]);Dfs(S, res, v, i + 1);v.pop_back();}}}
};

  To solve this problem, it is helpful to first think how many subsets are there. If there is no duplicate element, the answer is simply 2^n, where n is the number of elements. This is because you have two choices for each element, either putting it into the subset or not. So all subsets for this no-duplicate set can be easily constructed:

num of subset

  • (1 to 2^0) empty set is the first subset
  • (2^0+1 to 2^1) add the first element into subset from (1)
  • (2^1+1 to 2^2) add the second element into subset (1 to 2^1)
  • (2^2+1 to 2^3) add the third element into subset (1 to 2^2)
  • ....
  • (2^(n-1)+1 to 2^n) add the nth element into subset(1 to 2^(n-1))

Then how many subsets are there if there are duplicate elements? We can treat duplicate element as a spacial element. For example, if we have duplicate elements (5, 5), instead of treating them as two elements that are duplicate, we can treat it as one special element 5, but this element has more than two choices: you can either NOT put it into the subset, or put ONE 5 into the subset, or put TWO 5s into the subset. Therefore, we are given an array (a1, a2, a3, ..., an) with each of them appearing (k1, k2, k3, ..., kn) times, the number of subset is (k1+1)(k2+1)...(kn+1). We can easily see how to write down all the subsets similar to the approach above.

Solution 2 ()

class Solution {
public:vector<vector<int> > subsetsWithDup(vector<int> &S) {vector<vector<int> > res{{}};sort(S.begin(), S.end());for (int i = 0; i < S.size(); ) {int cnt = 0;while (cnt + i < S.size() && S[cnt + i] == S[i]) {++cnt;}int size = res.size();for (int j = 0; j < size; ++j) {vector<int> instance = res[j];for (int k = 0; k < cnt; ++k) {instance.push_back(S[i]);res.push_back(instance);}}i += cnt;}return res;}
};

Solution 3 ()

class Solution {
public:vector<vector<int> > subsetsWithDup(vector<int> &S) {vector<vector<int> > res{{}};sort(S.begin(), S.end());int size = 1;int last = !S.empty() ? S[0] : 0;for (int i = 0; i < S.size(); ++i) {if (last != S[i]) {last = S[i];size = res.size();}int newsize = res.size();for (int j = newsize - size; j < newsize; ++j) {res.push_back(res[j]);res.back().push_back(S[i]);}}return res;}
};

?

轉載于:https://www.cnblogs.com/Atanisi/p/6866474.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/392781.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392781.shtml
英文地址,請注明出處:http://en.pswp.cn/news/392781.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

多線程1

1-1 進程 程序是靜止的&#xff0c;運行中的程序就是進程。進程的三個特征&#xff1a; 動態性 &#xff1a; 進程是運行中的程序&#xff0c;要動態的占用內存&#xff0c;CPU和網絡等資源。獨立性 &#xff1a; 進程與進程之間是相關獨立的&#xff0c;彼此有自己的獨立內存區…

go 列出已經安裝的包_Go 安裝教程

一、在 Windows 上安裝 Go 環境首先在 Go 官網 下載 Windows 系統下的一鍵安裝包。然后雙擊打開該文件&#xff0c;一直點 Next 就行。注意這里默認是安裝到 C 盤&#xff0c;建議不要修改&#xff0c;因為環境變量會自動設置&#xff0c;如果安裝到其他盤&#xff0c;那么可能…

【轉】spin_lock、spin_lock_irq、spin_lock_irqsave區別

為什么80%的碼農都做不了架構師&#xff1f;>>> 轉自&#xff1a;http://blog.csdn.net/luckywang1103/article/details/42083613 void spin_lock(spinlock_t *lock);void spin_lock_irq(spinlock_t *lock);void spin_lock_irqsave(spinlock_t *lock, unsigned lon…

七年級計算機上教學計劃,初一教學計劃模板錦集5篇

初一教學計劃模板錦集5篇時光在流逝&#xff0c;從不停歇&#xff0c;我們又將迎來新的教學工作&#xff0c;我們要好好計劃今后的教育教學方法。那么一份同事都拍手稱贊的教學計劃是什么樣的呢&#xff1f;以下是小編為大家整理的初一教學計劃5篇&#xff0c;僅供參考&#xf…

程序員實際情況_程序員實際上是做什么的?

程序員實際情況What do programmers actually do? What can they be working on?程序員實際上是做什么的&#xff1f; 他們可以做什么&#xff1f; In this video from an Airbnb software engineer, you will learn about what programmers do on a day-to-day basis. She …

leetcode 1365. 有多少小于當前數字的數字(排序)

給你一個數組 nums&#xff0c;對于其中每個元素 nums[i]&#xff0c;請你統計數組中比它小的所有數字的數目。 換而言之&#xff0c;對于每個 nums[i] 你必須計算出有效的 j 的數量&#xff0c;其中 j 滿足 j ! i 且 nums[j] < nums[i] 。 以數組形式返回答案。 示例 1&…

spring整合springmvc案例

面試遇到過上機操作&#xff0c;不知道小伙伴們遇到過沒。 案例。 1、新建web項目&#xff0c;找到相關的jar包。 轉載于:https://www.cnblogs.com/sjzxs/p/11158116.html

我的世界服務器玩家在線時間,將公布上線時間?我的世界中國版網易520前瞻

【17173專稿&#xff0c;轉載請注明出處】《我的世界》中國版最近一段時間動作不斷。網易CEO丁磊在財報電話會議上公布了《我的世界》手游版會在7月份推出&#xff0c;結合《我的世界》中國版的公告提及&#xff1a;”《我的世界》中國版即將在暑期上線“。如此看來手游版和PC版…

ftpwebrequest 無法加載或初始化請求的服務提供程序_jvm之類加載機制

什么是類加載每個編寫的".java"拓展名類文件都存儲著需要執行的程序邏輯&#xff0c;這些".java"文件經過Java編譯器編譯成拓展名為".class"的文件&#xff0c;".class"文件中保存著Java代碼經轉換后的虛擬機指令&#xff0c;當需要使…

【284天】我愛刷題系列(43)

叨叨兩句 身體是靈魂的載體&#xff0c;靈魂是身體的指引&#xff0c;用心維護、馴化你的身體&#xff0c;構建通道&#xff0c;指引它將力量與情緒宣泄在你想做出成績的領域&#xff0c;神奇的事情就會發生&#xff0c;哈哈。牛客網——java專項練習023 1 SuppressWarnings(“…

基于python滲透測試_Python中基于屬性的測試簡介

基于python滲透測試by Shashi Kumar Raja由Shashi Kumar Raja Python中基于屬性的測試簡介 (Intro to property-based testing in Python) In this article we will learn a unique and effective approach to testing called property-based testing. We will use Python , p…

leetcode144. 二叉樹的前序遍歷(迭代)

給定一個二叉樹&#xff0c;返回它的 前序 遍歷。示例:輸入: [1,null,2,3] 1\2/3 輸出: [1,2,3]代碼 /*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode() {}* TreeNode(int val…

矩陣的理解經典博客

矩陣理解一&#xff1a;https://blog.csdn.net/myan/article/details/647511 矩陣理解二&#xff1a;https://blog.csdn.net/myan/article/details/649018 矩陣理解三&#xff1a;https://blog.csdn.net/myan/article/details/1865397 關鍵結論&#xff1a; 1. 首先有空間&…

推斷圖片格式

Linux/Unix下系統推斷文件類型并不根據文件名稱&#xff0c;也即不會根據文件后綴來推斷文件的類型。從網上下載了一個圖片&#xff0c;沒有后綴&#xff0c;希望可以正確推斷出格式。以便于共享到其它平臺&#xff0c;該怎么辦呢&#xff1f; 不同文件類型的文件頭部信息不同&…

云服務器怎么設置域名,云服務器域名設置在哪里

可能不同的云服務廠商域名設置的方式略有不同&#xff0c;不過&#xff0c;大體來講&#xff0c;方法應該都差不多的。下面我們以1.打開瀏覽器&#xff0c;搜索西部數碼官網并登陸賬號密碼&#xff0c;到會員中心。2.進入管理中心后&#xff0c;在左側的業務管理中找到3.點擊服…

RHCE 學習筆記(9) 網絡管理

n這一節本來按照教學大綱應該是學習SSH&#xff0c;不過SSH有很多網絡相關的知識&#xff0c;因此老師把網絡內容提前了一些。網絡的基本知識例如IP&#xff0c;DNS&#xff0c;DHCP&#xff0c;路由協議等常識就不在此解釋了。 RHEL查看網卡的相關信息很容易&#xff0c;ifcon…

leetcode 1207. 獨一無二的出現次數(map+set)

給你一個整數數組 arr&#xff0c;請你幫忙統計數組中每個數的出現次數。 如果每個數的出現次數都是獨一無二的&#xff0c;就返回 true&#xff1b;否則返回 false。 示例 1&#xff1a; 輸入&#xff1a;arr [1,2,2,1,1,3] 輸出&#xff1a;true 解釋&#xff1a;在該數組…

地圖上繪制任意角度的橢圓_地圖上的總橢圓

地圖上繪制任意角度的橢圓或者&#xff0c;如何選擇下班后去海灘的最佳方式 (Or, how to choose the best way to walk to the beach after work) It was a cool autumn evening when Hila Kloper and I were thinking of going to the beach after work. The beach is about 2…

【NOI2014】起床困難綜合癥 貪心

從高到低按位貪心&#xff0c;討論一下初始0或1&#xff0c;分別暴力算出結果是什么 如果一開始0就能得1當然直接ans壘起來 如果1能得1而且當前m夠用&#xff0c;那也壘起來&#xff0c;同時m減掉 否則gg 2min的代碼 1 #include <bits/stdc.h>2 #define miaom(x,y) ((x &…

用原生js封裝get方法

get方法的封裝 首先我們看一下用原生js來發送請求的步驟: 1.創建請求對象 .var xhrnew XMLHttpRequest(); 2.創建open方法確認請求方式和地址 xhr.open(get,url) ps(記住get方法有參數的話在url后面用?符號連接再加上參數如:url?num3,多個參數用&符號連接); 3.監聽事件…