给定一个可包含重复数字的序列,返回所有不重复的全排列。

示例:

输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ]

在全排列1题目的基础上先排序,目的是把相同的元素聚在一起,然后在递归完一个数时,把后面与他相同的数给过滤掉,防止重复

class Solution {
public:
vector<vector<int> >res;
vector<int> visit;
int size;
vector<vector<int> > permuteUnique(vector<int>& nums)
{
int len = nums.size();
if(len == 0)
return res;
sort(nums.begin(), nums.end());
size = len;
visit = vector<int>(len, 0);
vector<int> temp;
DFS(nums, temp, 0);
return res;
} void DFS(vector<int>& nums, vector<int> &temp, int len)
{
if(len == size)
{
res.push_back(temp);
}
for(int i = 0; i < size; i++)
{
if(visit[i] == 1)
continue;
visit[i] = 1;
temp.push_back(nums[i]);
DFS(nums, temp, len + 1);
visit[i] = 0;
temp.pop_back();
for(; i < size - 1; i++)
{
if(nums[i] == nums[i + 1])
continue;
else
break;
}
}
}
};

Leetcode47. Permutations II全排列2的更多相关文章

  1. leetcode47. Permutations II

    leetcode47. Permutations II 题意: 给定可能包含重复的数字的集合,返回所有可能的唯一排列. 思路: 这题全排列两种写法. 用hash表记录已经visit的数字,虽然看起来很 ...

  2. [LeetCode] Permutations II 全排列之二

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  3. [LeetCode] 47. Permutations II 全排列之二

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  4. [Leetcode] permutations ii 全排列

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  5. LeetCode47.Permutations II(剑指offer38-1)

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  6. [LeetCode] 47. Permutations II 全排列 II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  7. permutations II(全排列 2)

    题目要求 Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  8. 47. Permutations II (全排列有重复的元素)

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  9. leetcode Permutations II 无重全排列

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...

随机推荐

  1. 信息安全-浏览器-CORS:CORS(跨域资源共享)

    ylbtech-信息安全-浏览器-CORS:CORS(跨域资源共享) 1.返回顶部 1. CORS,全称Cross-Origin Resource Sharing,是一种允许当前域(domain)的资 ...

  2. RvmTranslator7.2

    1. RvmTranslator7.2 增加一个视图方块,方便视图切换; Download: https://github.com/eryar/RvmTranslator/releases/tag/7 ...

  3. shell 的基本理解

    shell 事先通过一个变量设定好了多个路径,当用户输入命令时,shell会自动到这些路径(由左向右)以此查找 与命令名称相同的可执行文件 hash 用来保存以前曾经执行过的命令,以哈希表的方式保存, ...

  4. Linux 下的终端

    终端: 1 虚拟终端 ctrl + alt + F(1-6) : ctrl + alt + F7 : 图形终端 启动图形终端 : Gnome  :  #startx   或者  #startx &am ...

  5. 解决编译GCC内存不足的错误

    近期在使用阿里和腾讯的云服务器,由于只是测试用所以只租用了廉价512的内存,在编译gcc时遇到错误,表面上看只是编译错误,并且原因不明,纠结了几次之后猜测应该是由于系统资源不足导致的,所以尝试增加系统 ...

  6. [原创]新版PageOffice V4.0为什么用弹出窗口的方式打开文件?

    前的包含文档处理功能的Web办公系统,在打开文档的时候,一部分系统是采用Office文档嵌入到主窗口页面中右侧工作区域的方式,另一部分系统采用的是弹出新的浏览器窗口,里面完整的嵌入Office文件的打 ...

  7. python3-常用模块之openpyxl(1)

    1.创建工作簿 from openpyxl import Workbook # 创建excel对象 wb = Workbook() # 获取第一个sheet = wb.active # 单元格写入内容 ...

  8. <每日一题>题目15:mysql创建表及相关约束

    题目: 解答: 第一个表创建: create table class( cid int not null auto_increment primary key, caption char(20) no ...

  9. iBatis 代码自动生成工具 iBator 及 Example 使用

    iBator的下载和安装 官方下载地址:http://people.apache.org/builds/ibatis/ibator/ 安装:见<Eclipse 插件安装> 安装完成后,“F ...

  10. CodeChef TRIPS-Children Trips 树上分块

    参考文献国家集训队2015论文<浅谈分块在一类在线问题的应用>-邹逍遥 题目链接 题目大意 一棵n个节点的树,树的每条边长度为1或2,每次询问x,y,z. 要求输出从x开始走,每次只能走到 ...