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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 函数式Android编程(II):Kotlin语言的集合操作

    原文标题:Functional Android (II): Collection operations in Kotlin 原文链接:http://antonioleiva.com/collectio ...

  4. 统计分析中Type I Error与Type II Error的区别

    统计分析中Type I Error与Type II Error的区别 在统计分析中,经常提到Type I Error和Type II Error.他们的基本概念是什么?有什么区别? 下面的表格显示 b ...

  5. hdu1032 Train Problem II (卡特兰数)

    题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能.    (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...

  6. [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 ...

  7. [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 ...

  8. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  9. [LeetCode] Permutations II 全排列之二

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

随机推荐

  1. POJ 2540 Hotter Colder

    http://poj.org/problem?id=2540 题意:给你每次行走的路径,而且告诉你每次离一个点光源是远了还是近了,要求每次光源可能存在的位置的面积. 思路:如果出现"same ...

  2. Codeforces 455B A Lot of Games

    http://codeforces.com/contest/455/problem/B 题目大意: 给出n个字符串,进行k次游戏,每次游戏输家下次作为先手,游戏规则为每次放一个字母,导致当前构造的字符 ...

  3. 硬盘安装windows7

    微软已经发表声明 Windows XP 操作系统将于2014年4月8日停止提供补丁和安全更新,提醒用户尽快升级现有的XP操作系统.Windows XP曾在2001年10月25日正式发布的,已经走过了十 ...

  4. 黑马程序员_Java_多线程

    8.多线程 8.1.多线程概述 进程:是一个正在执行中的程序.每一个进程执行都有一个执行顺序.该顺序是一个执行路径,或者叫一个控制单元. 线程(例:FlashGet):就是进程中一个独立的控制单元.线 ...

  5. cf492D Vanya and Computer Game

    D. Vanya and Computer Game time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  6. 微博OpenAPI练习之问题记录

    今日想通过新浪微博OpenAPI,做一个客户端出来.可以说过程比较艰难.这里只记录下遇到的问题,其它的按api要求注册.创建应用什么就好了. 1.API jar引用问题 创建了自己的工程,并按照文档说 ...

  7. java DI 框架spring(web)、Guice(web)、Dagger&Dagger2(android)

    java DI 框架spring(web).Guice(web).Dagger&Dagger2(android) (待续...)

  8. java获取项目地址或tomcat绝对地址

    在java项目中获取文件的路径,不管是相对路径还是绝对路径,其本质都是通过绝对路径去寻找. 获取项目地址 request.getSession().getServletContext().getRea ...

  9. eclispe远程调试tomcat

    在eclispe中新建web应用,名字叫webtest.里面只有一个HelloServlet.Web.xml配置如下. 修改tomcat的启动脚本startup.bat.复制startup.bat为s ...

  10. CSS介绍

    从HTML被发明开始,样式就以各种形式存在.不同的浏览器结合它们各自的样式语言为用户提供页面效果的控制.最初的HTML只包含很少的显示属性. 随着HTML的成长,为了满足页面设计者的要求,HTML添加 ...