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. Ansible简介及常用模块

    一.基础介绍 1.简介 ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabric)的优点,实现了批量系统配置. ...

  2. tamper绕WAF详解

    0x00 背景 sqlmap中的tamper脚本来对目标进行更高效的攻击. 由于乌云知识库少了sqlmap-tamper 收集一下,方便学习. 根据sqlmap中的tamper脚本可以学习过绕过一些技 ...

  3. maven jar包库

    如果你的项目不是maven项目,比如ant,你的项目需要某些jar包的时候可以到maven 的jar包中心库下载 地址:http://search.maven.org/ http://mvnrepos ...

  4. unslider的用法详解

    unslider本身下载后只需要dist文件夹就好了, 其中只包含dist/js/unslider-min.js, jquery的js要自己提供; dist/css/unslider.css是主要的c ...

  5. 如何在本地配置php分析工具xhprof

    测试环境: linuxMint + nginx1.4.6+mysql5.5+php5.5 什么是xhprof? XHProf是一个分层PHP性能分析工具.它报告函数级别的请求次数和各种指标,包括阻塞时 ...

  6. Linux下用于查看系统当前登录用户信息的4种方法

    1. 使用w命令查看登录用户正在使用的进程信息 w命令用于显示已经登录系统的用户的名称,以及他们正在做的事.该命令所使用的信息来源于/var/run/utmp文件.w命令输出的信息包括: 用户名称 用 ...

  7. Java实验1-文件IO

    目标:掌握Java类的创建,Java  I/O操作,Java集合类的使用等 内容: 王老师非常喜欢读书,为了便于查阅,他每次买书回家后就在笔记本上登记每本书的详细信息(书名.作者.出版社.出版日期.价 ...

  8. [歪谈]我们该怎么正确面对"批评"

    这两天看到网上有类似这样的话题:遇到批评你是如何面对? 其实标题中没有“领导”,并不是专指:遇到“领导”批评你是如何面对? 在IT界(其他行业和领域就不谈了).         批评分三个层面: 1. ...

  9. 通过Canvas及File API缩放并上传图片完整示例

    <!DOCTYPE html> <html> <head> <title>通过Canvas及File API缩放并上传图片</title> ...

  10. .NET逻辑分层架构总结

    一.基础知识准备: 1.层的原则: (1)每一层以接口方式供上层调用. (2)上层只能调用下层. (3)依赖分为松散交互和严格交互两种. 2.业务逻辑分类: (1)应用逻辑. (2)领域逻辑. 3.采 ...