class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> ans = new ArrayList<List<Integer>>();
helper(nums, 0, ans, new ArrayList<Integer>());
return ans;
}
private void helper(int[] nums, int idx, List<List<Integer>> ans, ArrayList<Integer> path){
if(idx == nums.length) {
ans.add(new ArrayList<Integer>(path));
return;
}
Set<Integer> selected = new HashSet<Integer>();
for(int i = idx; i < nums.length; i++){
if(selected.contains(nums[i])) continue;
path.add(nums[i]);
selected.add(nums[i]);
swap(nums, i, idx);
helper(nums, idx+1, ans, path);
swap(nums, i, idx);
path.remove(path.size()-1);
}
return;
}
private void swap(int[] nums, int a, int b){
int tmp = nums[a];
nums[a] = nums[b];
nums[b] = tmp;
}
}
47 permutation II(with duplicate)
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來骏啰,“玉大人节吮,你說我怎么就攤上這事∨懈” “怎么了透绩?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長壁熄。 經(jīng)常有香客問我帚豪,道長,這世上最難降的妖魔是什么草丧? 我笑而不...
- 正文 為了忘掉前任狸臣,我火速辦了婚禮,結(jié)果婚禮上昌执,老公的妹妹穿的比我還像新娘烛亦。我一直安慰自己,他們只是感情好懂拾,可當我...
- 文/花漫 我一把揭開白布煤禽。 她就那樣靜靜地躺著,像睡著了一般委粉。 火紅的嫁衣襯著肌膚如雪呜师。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼角寸,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了忿墅?” 一聲冷哼從身側(cè)響起扁藕,我...
- 正文 年R本政府宣布儒陨,位于F島的核電站花嘶,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏框全。R本人自食惡果不足惜察绷,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望津辩。 院中可真熱鬧拆撼,春花似錦、人聲如沸喘沿。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽蚜印。三九已至莺禁,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間窄赋,已是汗流浹背哟冬。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 217. Contains Duplicate https://leetcode.com/problems/con...
- Given an array of integers and an integer k, find out whe...
- 原題鏈接:Contains Duplicate II這題的答案是我在leetcode官方論壇上找的,我自己寫的代碼...
- You may find that screen resolution is not same as parent...