/*
90. Subsets II
My Submissions
Question
Editorial Solution
Total Accepted: **69575** Total Submissions: **224792** Difficulty: **Medium**
Given a collection of integers that might contain duplicates, ***nums***, return all possible subsets.
**Note:** The solution set must not contain duplicate subsets.
For example,
If ***nums*** = [1,2,2], a solution is:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
Hide Company Tags Facebook
Hide Tags Array Backtracking
*/
public class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
//https://leetcode.com/discuss/54544/very-simple-and-fast-java-solution
/*
The Basic idea is: use "while (i < n.length && n[i] == n[i - 1]) {i++;}" to avoid the duplicate. For example, the input is 2 2 2 3 4. Consider the helper function. The process is:
each.add(n[i]); --> add first 2 (index 0)
helper(res, new ArrayList<>(each), i + 1, n); --> go to recursion part, list each is <2 (index 0)>
while (i < n.length && n[i] == n[i - 1]) {i++;} --> after this, i == 3, add the element as in subset I
*/
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
List<Integer> cur = new ArrayList<>();
helper(res, cur, 0, nums);
return res;
}
public void helper(List<List<Integer>> res, List<Integer> cur, int pos, int[] n) {
if (pos <= n.length) {
res.add(cur);
}
int i = pos;
while (i < n.length) {
cur.add(n[i]);
helper(res, new ArrayList<>(cur), i+1, n);
cur.remove(cur.size() - 1);
i++;
while( i < n.length && n[i] == n[i-1]) {
i++;
}
}
return;
}
}
90. Subsets II
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門戈锻,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人却嗡,你說(shuō)我怎么就攤上這事舶沛。” “怎么了窗价?”我有些...
- 文/不壞的土叔 我叫張陵如庭,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我撼港,道長(zhǎng)坪它,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任帝牡,我火速辦了婚禮往毡,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘靶溜。我一直安慰自己开瞭,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開白布罩息。 她就那樣靜靜地躺著嗤详,像睡著了一般。 火紅的嫁衣襯著肌膚如雪瓷炮。 梳的紋絲不亂的頭發(fā)上葱色,一...
- 那天,我揣著相機(jī)與錄音娘香,去河邊找鬼苍狰。 笑死,一個(gè)胖子當(dāng)著我的面吹牛烘绽,可吹牛的內(nèi)容都是我干的淋昭。 我是一名探鬼主播,決...
- 文/蒼蘭香墨 我猛地睜開眼诀姚,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼响牛!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起,我...
- 序言:老撾萬(wàn)榮一對(duì)情侶失蹤呀打,失蹤者是張志新(化名)和其女友劉穎矢赁,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體贬丛,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡撩银,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年只嚣,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了荷愕。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
- 正文 年R本政府宣布境肾,位于F島的核電站,受9級(jí)特大地震影響胆屿,放射性物質(zhì)發(fā)生泄漏奥喻。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一非迹、第九天 我趴在偏房一處隱蔽的房頂上張望环鲤。 院中可真熱鬧,春花似錦憎兽、人聲如沸冷离。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)酒朵。三九已至,卻和暖如春扎附,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背结耀。 一陣腳步聲響...
- 正文 我出身青樓碍粥,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親黑毅。 傳聞我的和親對(duì)象是個(gè)殘疾皇子嚼摩,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- Given a collection of integers that might contain duplica...
- Given a collection of integers that might contain duplica...
- Given a collection of integers that might contain duplica...
- 算命的說(shuō),你們八字不合潮秘。 2016-10-16 執(zhí)些 執(zhí)些 曉夕覺得二狗什么都好琼开,就是命不好,這個(gè)命不好枕荞,不是說(shuō)學(xué)...