作者: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. 创建、显示和删除保存的用户名和密码(cmdkey)

    创建,显示和删除保存的用户名和密码: cmdkey.exe /add:targetname /user:username /pass:password

  2. TigerDLNA for ios 集成Tlplayer

    好久没有写博客了,这次带着TigerDLNA for ios 跟大家见面 什么都不说先上图 1.优点 优点由于libTigerDLNA使用uiview封装,所以大家可以很方便的集成到自己的项目中.由于 ...

  3. iOS开发——新特性OC篇&Swift 2.0新特性

    Swift 2.0新特性     转眼间,Swift已经一岁多了,这门新鲜.语法时尚.类型安全.执行速度更快的语言已经渐渐的深入广大开发者的心.我同样也是非常喜爱这门新的编程语言. 今年6月,一年一度 ...

  4. Mysql数据库导出压缩并保存到指定位置备份脚本

    #!/bin/bashbackdir=/home/shaowei/dbbakdbuser='dbusername'dbpass='dbpasswd'dblist=$(ls -p /var/lib/my ...

  5. jQuery执行请求demo

    Ajax教程地址 4.3.2 执行GET请求 使用jQuery来执行GET请求有多种可选的方法.最简单的方法是使用$.get(),其参数是一个URL和一个回调函数:该回调函数有两个参数:一个是从服务器 ...

  6. php开启新的进程或者线程

    开启线程: $php_cli_path = '/usr/bin/php';$dir_path = dirname(__FILE__)."/".'CheckTaskState.she ...

  7. ajax检查用户名

    Ajax实现的效果 究竟Ajax能实现什么功能呢?今天下午学习了一下Ajax,现在跟大家分享一下我的学习心得.Ajax是什么?工作机制又是什么?可能不大准确,只是我个人看了视频学习后的一点点看法. A ...

  8. 解读eXtremeComponents代码结构--转载

    原文地址:http://blog.csdn.net/lark3/article/details/1937466 大致整理了去年写的东西,罗列如下: ec是一系列提供高级显示的开源JSP定制标签,当前的 ...

  9. XMemcached使用示例--转

    Memcached 是一个高性能的分布式内存对象的key-value缓存系统,用于动态Web应用以减轻数据库负载,现在也有很多人将它作为内存式数据库在使用,memcached通过它的自定义协议与客户端 ...

  10. Spring任务调度

    任务调度是大多数应用系统的常见需求之一,拿论坛来说:每个半个小时生成精华文章的RSS文件,每天凌晨统计论坛用户的积分排名,每隔30分钟执行对锁定过期的用户进行解锁.以上都是以时间为关注点的调度,事实上 ...