47. Permutations II (Back-Track, Sort)
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and [2,1,1].
思路:有重复数字的情况,之前在Subsets II,我们采取的是在某一个递归内,用for循环处理所有重复数字。这里当然可以将数组排序,然后使用该方法。
而另一种方法是不排序,在一个递归内申请一个set,用来判断该数字是否已经在当前depth出现过:如果nums[depth]出现过,那么说明这个前缀在之前的遍历已存在,不需要再进行讨论。
class Solution {
public:
vector<vector<int> > permuteUnique(vector<int> &num) {
result.clear();
dfs(num, );
return result;
}
void dfs(vector<int> num, int depth)
{
if(depth == num.size()-)
{
result.push_back(num);
return;
}
dfs(num,depth+);
set<int> flag; //用来判断当前数字是否在depth位置出现过
flag.insert(num[depth]);
int temp = num[depth];
for(int i = depth+; i< num.size(); i++)
{
if(flag.find(num[i])!=flag.end()) continue;
flag.insert(num[i]);
num[depth]=num[i];
num[i] = temp;
dfs(num,depth+);
num[i]=num[depth];
num[depth]=num[i];
}
}
private:
vector<vector<int> > result;
};
思路II:同样可以用insertion sort的方法
碰到相同元素,break for循环。注意不是continue,因为假设要插入的元素nums[i]与result[resultIndex][k]相同, 那么这个result[resultIndex][k]已经在上一轮的时候,对{0...k-1}的位置已经做过插入排序。如果再进行插入,会造成相同的前缀,导致重复result。
class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
int size = nums.size();
int resultSize;
int resultIndex;
int count;
vector<vector<int>> result;
vector<int> resultItem(,nums[]);
result.push_back(resultItem);
for(int i = ; i <size; i++){ //nums[i] is the num to insert
resultSize = result.size(); //resultSize in the preceeding insert iterate
for(int j = ; j < resultSize; j++){ //iterate the array to do insertion
result[j].push_back(nums[i]);
resultIndex = j;
for(int k = i-; k >=; k--){ //like insertion sort, adjust forward
if(nums[i]==result[resultIndex][k]) break; //equal element, don't insert
result.push_back(result[resultIndex]);
result[result.size()-][k+] = result[resultIndex][k];
result[result.size()-][k] = result[resultIndex][k+];
resultIndex = result.size()-;
}
}
}
return result;
}
};
47. Permutations II (Back-Track, Sort)的更多相关文章
- [Leetcode][Python]47: Permutations II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...
- leetcode46. Permutations 、47. Permutations II、 剑指offer字符串的排列
字符串排列和PermutationsII差不多 Permutations第一种解法: 这种方法从0开始遍历,通过visited来存储是否被访问到,level代表每次已经存储了多少个数字 class S ...
- 【LeetCode】47. Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- leetCode 47.Permutations II (排列组合II) 解题思路和方法
Permutations II Given a collection of numbers that might contain duplicates, return all possible un ...
- [LeetCode] 47. Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [leetcode] 47. Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode 【47. Permutations II】
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 47. Permutations II
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- 47. Permutations II(medium, backtrack, 重要, 条件较难思考)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
随机推荐
- Alpha冲刺一(7/10)
前言 队名:拖鞋旅游队 组长博客:https://www.cnblogs.com/Sulumer/p/10013652.html 作业博客:https://edu.cnblogs.com/campus ...
- ROS会议 ROSCon 2017
----ROSCon2012-2017----来源链接:https://roscon.ros.org 近三年ROSCon(2015-2017)都会将会议视频录像和文档公开~以下为机 ...
- VMware上安装CenterOS
1.环境:Win10.VMware Workstation 12.Centeros 7 2.VMware workstation12安装 双击“VMware_workstation_full_12.5 ...
- word 使用中 上标符号的实现
1. 首先在word 中打下一段话 如: 啦啦啦啦啦啦啦啦 然后加入你需要的上标 如 [2] 2. 选中你需要的上标,然后右击 3. 点击字体选项 出现下图: 4. 在 ...
- CF1056:Check Transcription(被hack的hash)
One of Arkady's friends works at a huge radio telescope. A few decades ago the telescope has sent a ...
- BZOJ5091: [Lydsy1711月赛]摘苹果(简单概率)
5091: [Lydsy1711月赛]摘苹果 Time Limit: 1 Sec Memory Limit: 256 MBSubmit: 214 Solved: 163[Submit][Statu ...
- 使用阿里云docker加速器
登陆之后,在docker镜像仓库-加速器可获得专有加速地址. 如何使用Docker加速器 针对Docker客户端版本大于1.10的用户 您可以通过修改daemon配置文件/etc/docker/dae ...
- SpringBoot启动报:Caused by: java.lang.IllegalArgumentException: At least one JPA metamodel must be present!
使用spring boot对项目改造,启动报错: Caused by: java.lang.IllegalArgumentException: At least one JPA metamodel m ...
- 从event loop规范探究javaScript异步及浏览器更新渲染时机
异步的思考 event loops隐藏得比较深,很多人对它很陌生.但提起异步,相信每个人都知道.异步背后的“靠山”就是event loops.这里的异步准确的说应该叫浏览器的event loops或者 ...
- 10055 - Hashmat the Brave Warrior & 各数据类型所占字节数 (C语言)
Problem A Hashmat the brave warrior Input: standard input Output: standard output Hashmat is a brave ...