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 ...
随机推荐
- 2018.12.7 L190
China called for the immediate release of Meng Wanzhou, chief financial officer of Huawei Technologi ...
- 在mysql中,如何改变列声明.
C 在mysql中,如何改变列声明. 修改表 - 修改列名 使用 CHANGE COLUMN 来修改列的名字,还必须 设置 列的数据类型 mysql> desc test_tab -> / ...
- python中多进程
多进程 什么是进程 进程:正在进行的一个过程或者说一个任务,而负责执行任务的是CPU. 进程和程序的区别 程序仅仅是一堆代码而已,而进程指的是程序的运行过程. 举例 想象以为有着一手好厨艺的科学家肖亚 ...
- Opencv中Rect类
转载: Rect_类有些意思,成员变量x.y.width.height,分别为左上角点的坐标和矩形的宽和高.常用的成员函数有Size()返回值为一个Size,area()返回矩形的面积,contain ...
- springboot整合mybatis增删改查(一):项目创建
新建 打开 IDEA 工具,通过 File -> New -> Project->Spring Initializr 主要步骤包括: 选择 Spring Initializr 项目 ...
- BZOJ1568: [JSOI2008]Blue Mary开公司【李超树】
Description Input 第一行 :一个整数N ,表示方案和询问的总数. 接下来N行,每行开头一个单词"Query"或"Project". 若单词为Q ...
- 《DSP using MATLAB》Problem 2.9
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- SD卡驱动学习
https://blog.csdn.net/zqixiao_09/article/category/6163492 sd 协议解析: https://blog.csdn.net/g_salamande ...
- json-patch 了解
What is JSON Patch? JSON Patch is a format for describing changes to a JSON document. It can be used ...
- 禅道导出数据,excel打开为乱码处理
禅道里面导出的数据,用Excel打开是乱码(如图),如何解决? 第一步: 第二步: 第3步: 第4步: 第5步: 选择一个储存位置 最后的结果就是这样了