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. Python socket编程

    一.什么是socket: socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求 ...

  2. Xcode pch文件问题

    创建后需修改 Prefix Header Debug.Release 为:项目名称/PrefixHeader.pch/ #import <UIKit/UIKit.h>#import < ...

  3. idea的修改文件变颜色

    直接用IntelliJ IDEA 拉SVN 工程 时,如下图所示都不勾选可以成功拉取.

  4. Linq 中的 left join

    Linq 中的 left join 表A User: 表B UserType: Linq: from t in UserType join u in User on t.typeId equal u. ...

  5. strtoull函数的使用,及相关信息汇总

    kafka中涉及到序列化,有一个参数是在发送消息实体前边以一个序列号打头, seq = strtoull((prdcfgval+1), NULL, 10); do_seq = 1; 其中(prdcfg ...

  6. Eclipse构建Maven项目

    1. 安装m2eclipse插件     要用Eclipse构建Maven项目,我们需要先安装meeclipse插件     点击eclipse菜单栏Help->Eclipse Marketpl ...

  7. JavaScript-也来谈--闭包

    闭包,以前研究过,可能是当初理解的不够透彻,现在又忘了,(给自己一个台阶下`-...)毕竟js一直没怎么用, 为了防止自己过段时间再忘了,写篇重要的闭包重点, 这样时不时也能温习下知识,不用每次想了解 ...

  8. PHP安装laravel(win+linux)

    作为一名不优秀的程序猿,忙碌的四月终于结束了,五一大假的最后一天,终于有时间来整理整理这段时间的收获了. 一.laravel介绍 首先看看http://www.sitepoint.com/网站做的一个 ...

  9. HDOJ 1520 Anniversary party

    树形DP....在树上做DP....不应该是猴子干的事吗?  Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Li ...

  10. 虚拟机安装Ubuntu三种网络模式

    VMWare提供三种工作模式桥接(bridge).NAT(网络地址转换)和host-only(主机模式). NAT(网络地址转换) 在NAT模式下,虚拟系统需要借助NAT(网络地址转换)功能,通过宿主 ...