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 ...
随机推荐
- 戴着镣铐起舞——从logo设计说起
一天中午走在路上,顺道去看了学校新媒体艺术与设计系的学生优秀作品展.看到一个logo设计时引发了我很大的兴趣,觉得设计的非常清新活泼,不过并没有拍下来,在这里不能贴图.但是logo设计大家应该都有所概 ...
- HDU--3487 Play with Chain (Splay伸展树)
Play with Chain Problem Description YaoYao is fond of playing his chains. He has a chain containing ...
- HDU---4417Super Mario 树状数组 离线操作
题意:给定 n个数,查询 位置L R内 小于x的数有多少个. 对于某一次查询 把所有比x小的数 ”的位置“ 都加入到树状数组中,然后sum(R)-sum(L-1)就是答案,q次查询就要离线操作了,按高 ...
- java 面试基础典型题及答案
1.switch能否作用在byte.int.long.String? 答案:switch能作用在byte.int.enum常量, 补充:jdk7可以作用在String上 2.short s = 1; ...
- java_final
- html5 app图片预加载
function Laimgload(){} //图片预加载JS Laimgload.prototype.winHeight = function(){ //计算页面高度 var winHeight ...
- 【git 问题小说说】 git add时候报错:LF will be replaced by CRLF
本文来自:http://blog.csdn.net/loovejava/article/details/22114477 最近工作在window平台,不怎么使用命令行了所以导致很多命令都不熟悉啦 哈哈 ...
- [置顶] iOS 应用程序内部国际化,不跟随系统语言
前言:网络上关于iOS国际化的文章很多,但基本上都是基于跟随系统语言的国际化,笔者就不赘述了-0 – 今天要讲的是不跟随系统的切换语言版本方案,即程序内部的切换语言版本方案. 一.总则: 应用内部语言 ...
- C++11 多线程 基础
C++11开始支持多线程编程,之前多线程编程都需要系统的支持,在不同的系统下创建线程需要不同的API如pthread_create(),Createthread(),beginthread()等,使用 ...
- ARM体系结构_DAY2
程序状态寄存器(CPSR) Mode位[4:0]:处理器模式为 USER模式不能直接切换到特权模式,在特权模式下可以直接修改mode位[4:0]为10000,切换到USER模式. T bit位[5]: ...