LeetCode_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].
DFS + 剪枝
class Solution {
public:
void DFS(vector<int> &num, int size,vector<int> temp)
{
if(size == n){
result.push_back(temp);
return ;
}
for(int i = ; i< n; i++)
{
if(flag[i] || (i!= &&flag[i-] && num[i] == num[i-] ) )
continue;
temp[size] = num[i];
flag[i] = true;
DFS(num, size+, temp);
flag[i] = false;
}
}
vector<vector<int> > permuteUnique(vector<int> &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
n = num.size();
result.clear();
sort(num.begin(), num.end());
if(n == ) return result;
flag.resize(n,false);
vector<int> temp(n,) ;
DFS(num,,temp);
return result ;
}
private :
int n;
vector<bool> flag;
vector<vector<int>> result;
};
这里解释下剪枝的原理: 有重复元素的时候,因为重复的元素处理无序所以导致重复,所以只要给重复的元素进入temp定义一个次序就可以去掉重复。这里定义次序的规则是: 先对所有元素排序对于有重复的元素,必须是排在后面的元素比排在前面的元素先进入temp
重写后,貌似比第一个版本要快一点
class Solution {
public:
void DFS(vector<int> &num, vector<int> &tp, vector<bool> flag)
{
if(num.size() == tp.size()){
res.push_back(tp);
return;
}
for(int i = 0; i< num.size(); i++)
{
if(flag[i] == true) continue;
if(i != 0 && num[i] == num[i-1] && flag[i-1] == false) continue;
flag[i] = true;
tp.push_back(num[i]);
DFS(num, tp, flag);
tp.pop_back();
flag[i] = false;
}
}
vector<vector<int> > permuteUnique(vector<int> &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
res.clear();
int len = num.size();
if(len < 1) return res;
sort(num.begin(), num.end());
vector<bool> flag(len, false);
vector<int> tp;
DFS(num, tp, flag);
return res;
}
private:
vector<vector<int>> res;
};
LeetCode_Permutations II的更多相关文章
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II
题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...
- 函数式Android编程(II):Kotlin语言的集合操作
原文标题:Functional Android (II): Collection operations in Kotlin 原文链接:http://antonioleiva.com/collectio ...
- 统计分析中Type I Error与Type II Error的区别
统计分析中Type I Error与Type II Error的区别 在统计分析中,经常提到Type I Error和Type II Error.他们的基本概念是什么?有什么区别? 下面的表格显示 b ...
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
- [LeetCode] Guess Number Higher or Lower II 猜数字大小之二
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...
- [LeetCode] Number of Islands II 岛屿的数量之二
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
随机推荐
- C#读取文件高效方法实现
C# Code 12345678910111213141516171819202122232425262728293031 private void button1_Click ...
- java比较相等符
public class Test1 { /** * @param args */ public static void main(String[] args){ int a = 1000, b = ...
- url中的jsessionid解释
(1) 这是一个保险措施 因为Session默认是需要Cookie支持的 但有些客户浏览器是关闭Cookie的 这个时候就需要在URL中指定服务器上的session标识,也就是5F4771183629 ...
- back_inserter的用法
1,代码如下: #include<iostream> #include<list> #include<algorithm> #include<iterator ...
- PHP 面向对象中常见关键字使用(final、static、const和instanceof)
PHP 面向对象中常见关键字的使用: 1.final :final关键字可以加在类或者类中方法之前,但是不能使用final标识成员属性. 作用: 使用final标识的类,不能被继承. 在类中使用fin ...
- 【Cocos2d-X开发学习笔记】第19期:动作管理类(CCActionManager)的使用
本系列学习教程使用的是cocos2d-x-2.1.4(最新版为3.0alpha0-pre) ,PC开发环境Windows7,C++开发环境VS2010 一.动作管理类 动作管理类CCActionMan ...
- Android UI开发详解之ActionBar .
在Android3.0之后,Google对UI导航设计上进行了一系列的改革,其中有一个非常好用的新功能就是引入的ActionBar,他用于取代3.0之前的标题栏,并提供更为丰富的导航效果. 一.添加A ...
- 内存测试工具memtester
Memtester是用户态工具,用于测试内存子系统的故障.非常方便,支持32位 或64位Unix-like系统.对于硬件开发开发者来说,memtester可以定位到物理地址. 1. 安装 下载地址ht ...
- 快速构建AdapterView的Adapter--ingeniousadapter
项目地址:ingeniousadapter 前面的话:本项目的原型是QuickAdapter,它们的思路基本一致,但本项目的优势在于: 支持AdapterView存在多个layout类型 可配置图片加 ...
- telnet IP不通/sybase central工具无法连接到数据库
问题描述:客户端sybase central工具无法连接到数据库 服务端操作系统:RHEL5.8_x64,安装sybase-ASE15.7,端口号4112 IP:192.168.1.220 hos ...