Permutations II

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].

 
先排序,如果一个元素与上一个元素相等,且前面没有使用该元素,则该元素不参与当前排列
 

 class Solution {
public:
vector<vector<int> > permuteUnique(vector<int> &num) { sort(num.begin(),num.end());
vector<vector<int> > result;
vector<int> tmp;
vector<bool> visited(num.size());
dfs(,num,result,visited,tmp);
return result; } void dfs(int level,vector<int> &num,vector<vector<int> > &result,vector<bool> &visited,vector<int> &tmp)
{
if(level==num.size())
{
result.push_back(tmp);
return;
} for(int i=;i<num.size();i++)
{
if(!visited[i])
{
if(i>=&&((num[i]==num[i-]&&visited[i-])||(num[i]!=num[i-]))||i==)
{
visited[i]=true;
tmp.push_back(num[i]);
dfs(level+,num,result,visited,tmp);
tmp.pop_back();
visited[i]=false;
}
}
}
} };
 

【leetcode】Permutations II的更多相关文章

  1. 【LeetCode】Permutations II 解题报告

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

  2. 【leetcode】Permutations II (middle)

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

  3. 【LeetCode】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

  4. 【LeetCode】课程表 II

    [问题]现在你总共有 n 门课需要选,记为 0 到 n-1.在选修某些课程之前需要一些先修课程.例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示他们: [0,1]给定课程总量以及 ...

  5. 【leetcode】Permutations

    题目描述: Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the ...

  6. 【leetcode】N-Queens II

    N-Queens II Follow up for N-Queens problem. Now, instead outputting board configurations, return the ...

  7. 【leetcode】Permutations (middle)

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  8. 【leetcode】Subsets II

    Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...

  9. 【LeetCode】Permutations(全排列)

    这道题是LeetCode里的第46道题. 题目要求: 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3 ...

随机推荐

  1. jsp日期插件My97DatePicker

    jsp日期插件My97DatePicker 强大的日期控件 使用方便简单 (2010-10-30 18:59:12) 转载▼ 标签: js javascript 日期插件 jsp it 分类: jqu ...

  2. [问题] UISearchBar 点击取消后跳动的问题

    问题详情: 首先是TableView 作为 NavigationController 的 RootViewContrller, 然后UISearchBar   添加到TableView 的 headV ...

  3. hive odbc

    C:\Windows\SysWOW64  

  4. thinkphp系统常量与自定义常量

    ----------------------------------------Action中使用的系统常量 ----------------------------------------THINK ...

  5. 浅谈JavaScript中的继承

    引言 在JavaScript中,实现继承的主要方式是通过原型链技术.这一篇文章我们就通过介绍JavaScript中实现继承的几种方式来慢慢领会JavaScript中继承实现的点点滴滴. 原型链介绍 原 ...

  6. 工具介绍 - NimbleText

    非常实用的工具, 即使不是程序员也有必要掌握这个简单的小工具. 这个工具有桌面版和在线版两个版本. 桌面版地址: http://nimbletext.com/ 在线版地址: http://nimble ...

  7. Tomcat 6 --- 使用Jasper引擎解析JSP

    熟悉JAVA web开发的朋友都知道JSP会被转换成java文件(预编译),然后编译成class使用,即按照JSP-->java-->class的过程进行编译. 由于JVM只认识class ...

  8. iOS:使用MVC模式帮ViewController瘦身

    如何给UIViewController瘦身 随着程序逻辑复杂度的提高,你是否也发现了App中一些ViewController的代码行数急剧增多,达到了2,3千行,甚至更多.这时如果想再添加一点功能或者 ...

  9. AlwaysOn可用性组功能测试(三)--其他测试

    三. 大数据量操作的时候发生的切换 1.对表进行大量插入,执行1千万遍,如下语句 insert into aa select * from sys.sysprocesses go 10000000 2 ...

  10. jquery隐藏按钮

    $(function () { jhbs = getQueryStringByName('jhbs'); shhbs = getQueryStringByName('shhbs'); if (shhb ...