【leetcode】Permutations II
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的更多相关文章
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【leetcode】Permutations II (middle)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- 【LeetCode】课程表 II
[问题]现在你总共有 n 门课需要选,记为 0 到 n-1.在选修某些课程之前需要一些先修课程.例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示他们: [0,1]给定课程总量以及 ...
- 【leetcode】Permutations
题目描述: Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the ...
- 【leetcode】N-Queens II
N-Queens II Follow up for N-Queens problem. Now, instead outputting board configurations, return the ...
- 【leetcode】Permutations (middle)
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- 【leetcode】Subsets II
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- 【LeetCode】Permutations(全排列)
这道题是LeetCode里的第46道题. 题目要求: 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3 ...
随机推荐
- centos 7.0 nginx 1.7.9 安装过程
系统用的是centos 7.0最小化安装 我现在安装完了 写一下步骤 还没完全搞懂 首先安装GCC [root@localhost ~]# yum install -y gcc gcc-c++ 已加载 ...
- iOS-马上着手开发iOS应用应用程序-第一部分介绍
教程:基础 main 中的 main.m 函数会调用自动释放池 (autorelease pool) 中的 UIApplicationMain 函数. @autoreleasepool { retur ...
- 小技能——markdown
如果常常要在电脑上写点东西,比如写笔记.做总结.写博客之类的,花一两个小时学会markdown还是很值的. markdown简介 markdown不是某个软件,而是一种标记语言,标记普通文本的格式,以 ...
- JQuery-EasyUI与EXTjs有什么区别?
一.ExtJS1.ExtJS可以用来开发RIA也即富客户端的AJAX应用,是一个用javascript写的,主要用于创建前端用户界面,是一个与后台技术无关的前端ajax框架.因此,可以把ExtJS用在 ...
- [MongoDB]Profiling性能分析
摘要 上篇文章介绍了mapReduce这个聚合操作.本篇将继续学习,db有了,collection和document也有,基本上够用了,但是随着项目上线后,发现业务数据越来越多,查询效率越来越慢,这时 ...
- default配置
log_format main '$remote_addr - $remote_user [$time_local] $request ' '"$status" $body_byt ...
- java项目命名规范
一.命名规范 1. 项目名全部小写 2. 包名全部小写 3. 类名首字母大写,如果类名由多个单词组成,每个单词的首字母都要大写. 如:public class MyFirstClass{} 4. 变量 ...
- vim 打开Linux下文件每一行后面都有^M的样式
由于服务器不是我一个人在操作,在修改apache配置文件时发现了一个很奇怪的问题,vim编辑打开配置文件发现后面都有一个^M的标记 虽然不会影响服务的运行,但总感觉不对劲,所以在此我尝试用替换的方式来 ...
- 机器码call和jmp地址的计算
call和jmp都是跳转指令,但是call的同时会把pc地址压入堆栈,并且这两种方式都有远和近跳转.下面的分析不全,因为没有在网上找到足够的资料,个人创造这个情景还是有些困难. 1.例子中的call的 ...
- Linux下安装和配置JDK与Tomcat(入门版)
JDK路径:/usr/java/jdk1.6.0_25 Tomcat路径:/usr/local/apache-tomcat 1. 下载jdk6.0(选择“.rpm.bin”结尾的,6u25版本) ht ...