作者: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. 导出cluster log

    将所有群集节点的日志导出到 clog 目录下: get-clusterlog -destination c:\clog 只导出前10分钟的群集日志: get-cluster -destination ...

  2. IOS - 常用宏定义和功能方法

    可能不定期添加新的东西 github地址:https://github.com/yuqingzhude/CommonUseDemo /************************Tools**** ...

  3. android 带表头,左右两个联动的ListView

    package com.rytong.mylist; import java.util.ArrayList; import java.util.HashMap; import java.util.Li ...

  4. Andropid自己定义组件-坐标具体解释

    在做一个view背景特效的时候被坐标的各个获取方法搞晕了,几篇抄来抄去的博客也没弄非常清楚. 如今把整个总结一下. 事实上仅仅要把以下这张图看明确就没问题了. watermark/2/text/aHR ...

  5. android126 zhihuibeijing 极光推送

    https://www.jpush.cn/ 张三把消息发送给自己的服务器,自己的服务器将消息发送给极光推送,然后极光推送将消息发送给妹子. 清单文件: <?xml version="1 ...

  6. NET中間語言(IL) 图解

    转载地址是:http://msdn.microsoft.com/zh-tw/library/dd229210.aspx 想查看IL指令,请看中英文对照表: CN-http://www.cnblogs. ...

  7. javascript中的 "=="

    对象之间比较比较的是引用地址 对象和其他比较,转成字符串 字符串和数字比较,字符串转成数字 布尔值和任何比较,转成数字 undefined == null NaN 和谁都不相等 javascript权 ...

  8. C++如何用system命令获取文件夹下所有文件名

    http://www.cplusplus.com/reference/cstdlib/system/ http://bbs.csdn.net/topics/30068943 #include < ...

  9. Forms and actions

    Forms and actions Adding new albums We can now code up the functionality to add new albums. There ar ...

  10. jqgrid 的编辑信息提示

    在编辑时,无外乎两种结果:成功和失败.在form edit的弹出编辑窗体中隐藏了两个单元(td),一个的ID是FormError,另一个没有id,有class叫做topinfo.就是这两个家伙可以分别 ...