作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html

题目链接:leetcode Permutations II 无重全排列

题目要求对有重复的数组进行无重全排列。为了保证不重复,类似于全排列算法使用dfs,将第一个数字与后面第一次出现的数字交换即可。例如对于序列1,1,2,2

有如下过程:

,1,2,2 -> 1,,2,2 -> 1,1,,2 -> 1,1,2,

           -> 1,2,,2 -> 1,2,1,

                -> 1,2,2,

    -> 2,,1,2 -> 2,1,,2 -> 2,1,1,

                -> 2,1,2,

           -> 2,2,,1 -> 2,2,1,

代码中使用set来记录数字是否在前面出现过,如果出现过,则不与第一个数字进行交换。

代码如下:

 class Solution {
public:
void swap(int &a, int &b)
{
int tmp = a;
a = b;
b = tmp;
}
void p(vector<int> &num, vector<vector<int> > &res, int begin)
{
if( begin == num.size() )
{
vector<int> tmp;
for( int i = ; i < num.size() ; i++ )
{
tmp.push_back(num[i]);
}
res.push_back(tmp);
}
else
{
set<int> used;
for( int i = begin ; i < num.size() ; i++ )
{
if(!used.count(num[i]))
{
used.insert(num[i]);
swap(num[i], num[begin]);
p(num, res, begin+);
swap(num[i], num[begin]);
}
}
}
}
vector<vector<int> > permuteUnique(vector<int> &num)
{
vector<vector<int> >res;
if( num.size() == )
{
return res;
}
sort(num.begin(), num.end());
p(num, res, );
return res;
}
};

leetcode Permutations II 无重全排列的更多相关文章

  1. LeetCode: Permutations II 解题报告

    Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...

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

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

  3. [Leetcode] permutations ii 全排列

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

  4. LeetCode Permutations II (全排列)

    题意: 给出n个元素(可能有重复的),请产生出所有的全排列. 思路: 同版本1的有点不同,这次有可能含有重复的元素,很容易就TLE,节省时间才是关键点. 如果将一个序列中两个相同的元素交换,这个序列是 ...

  5. [leetcode]Permutations II @ Python

    原题地址:https://oj.leetcode.com/problems/permutations-ii/ 题意: Given a collection of numbers that might ...

  6. leetcode -- Permutations II TODO

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

  7. [Leetcode] Permutations II

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

  8. [LeetCode] Permutations II 排列

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

  9. Leetcode之回溯法专题-47. 全排列 II(Permutations II)

    Leetcode之回溯法专题-47. 全排列 II(Permutations II) 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2] ...

随机推荐

  1. iOS开发--混编篇&swift与OC混合使用

    Swift与OC混合使用 swift 语言出来后,可能新的项目直接使用swift来开发,但可能在过程中会遇到一些情况,某些已用OC写好的类或封装好的模块,不想再在swift 中再写一次,哪就使用混编. ...

  2. java复习1 java简单介绍

    在学校的时候.学JAVA学的模棱两可,半知半解.工作以后给我带来了非常大的困扰,所以我须要在学一遍.如今就開始吧... . java[1]是一种能够撰写跨平台应用软件的面向对象的程序设计语言,是由Su ...

  3. android127 zhihuibeijing 屏幕适配

    ## 屏幕适配 ## 加载不同分辨率的图片是根据手机的像素来加载不同分辨率文件夹下的图片. > 先在主流屏幕来发: *(分辨率和手机屏幕大小无关), 遵循原则: 不用AbsoluteLayout ...

  4. LINUX SHELL条件判断

    算术运算的条件判断 [] [[]]: -eq -ne -lt -le -gt -ge (( )):><>=<== [root@monitor ~]# if (( 2 == 3) ...

  5. UNIX标准化及实现之基本系统数据类型

    历史上,某些UNIX系统变量已与某些C数据类型联系在一起.例如,历史上主.次设备号一直存放在一个16位的短整型中,8位表示主设备号,另外8位表示次设备号.但是,很多较大的系统需要用多于256个值来表示 ...

  6. C#_数据库交互_SqlHelper

    using System; using System.Collections.Generic; using System.Text; using System.Data.SqlClient; usin ...

  7. eclipse创建多模块maven工程小结

    创建maven工程步骤 1 新建一个maven工程,如下图所示: 2 选择项目名称(或项目目录),如下图所示: 3 填写maven工程相关信息,注意父maven工程的packing方式是pom,如下图 ...

  8. 手动开启tomacat服务器

    四.配置Tomcat环境变量 1,新建变量名(关键,你的tomacat安装目录):CATALINA_BASE,变量值:E:\apache-tomcat-6.0.37 2,新建变量名(关键,你的toma ...

  9. qt-vs-addin-版本支持

    qt-vs-addin-1.2.0-opensource.exe         VS200X qt-vs-addin-1.2.1-opensource.exe         VS200X qt-v ...

  10. Android版本控制系统及其间的差异

    一.何谓版本控制 它是一种软件工程籍以在开发的过程中,确保由不同人所编辑的同一档案都得到更新,它透过文档控制记录程序各个模块的改动,并为每次改动编上序号,并且编辑错误之后还可以回溯到以前的版本 二.可 ...