1. Given a collection of numbers that might contain duplicates, return all possible unique permutations.
  2.  
  3. For example,
  4. [1,1,2] have the following unique permutations:
  5. [1,1,2], [1,2,1], and [2,1,1].

  DFS + 剪枝

  1. class Solution {
  2. public:
  3. void DFS(vector<int> &num, int size,vector<int> temp)
  4. {
  5. if(size == n){
  6. result.push_back(temp);
  7. return ;
  8. }
  9. for(int i = ; i< n; i++)
  10. {
  11. if(flag[i] || (i!= &&flag[i-] && num[i] == num[i-] ) )
  12. continue;
  13.  
  14. temp[size] = num[i];
  15. flag[i] = true;
  16. DFS(num, size+, temp);
  17. flag[i] = false;
  18.  
  19. }
  20. }
  21. vector<vector<int> > permuteUnique(vector<int> &num) {
  22. // Start typing your C/C++ solution below
  23. // DO NOT write int main() function
  24. n = num.size();
  25. result.clear();
  26.  
  27. sort(num.begin(), num.end());
  28.  
  29. if(n == ) return result;
  30. flag.resize(n,false);
  31. vector<int> temp(n,) ;
  32. DFS(num,,temp);
  33.  
  34. return result ;
  35. }
  36. private :
  37. int n;
  38. vector<bool> flag;
  39. vector<vector<int>> result;
  40. };

这里解释下剪枝的原理: 有重复元素的时候,因为重复的元素处理无序所以导致重复,所以只要给重复的元素进入temp定义一个次序就可以去掉重复。这里定义次序的规则是: 先对所有元素排序对于有重复的元素,必须是排在后面的元素比排在前面的元素先进入temp

重写后,貌似比第一个版本要快一点

  1. class Solution {
  2. public:
  3. void DFS(vector<int> &num, vector<int> &tp, vector<bool> flag)
  4. {
  5. if(num.size() == tp.size()){
  6. res.push_back(tp);
  7. return;
  8. }
  9. for(int i = 0; i< num.size(); i++)
  10. {
  11. if(flag[i] == true) continue;
  12. if(i != 0 && num[i] == num[i-1] && flag[i-1] == false) continue;
  13.  
  14. flag[i] = true;
  15. tp.push_back(num[i]);
  16. DFS(num, tp, flag);
  17. tp.pop_back();
  18. flag[i] = false;
  19. }
  20. }
  21. vector<vector<int> > permuteUnique(vector<int> &num) {
  22. // Start typing your C/C++ solution below
  23. // DO NOT write int main() function
  24. res.clear();
  25. int len = num.size();
  26. if(len < 1) return res;
  27. sort(num.begin(), num.end());
  28. vector<bool> flag(len, false);
  29. vector<int> tp;
  30. DFS(num, tp, flag);
  31. return res;
  32.  
  33. }
  34. private:
  35. vector<vector<int>> res;
  36. };

  

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. BrainFuck语言生成器

    还要求生成的代码比较快和短. 当然stackexchange上面给出了实现,java的 http://codegolf.stackexchange.com/questions/5418/brainfu ...

  2. 【hihoCoder第十五周】最近公共祖先·二

    老实说我没有读题,看见标题直接就写了,毕竟hiho上面都是裸的算法演练. 大概看了下输入输出,套着bin神的模板,做了个正反map映射,但是怎么都得不了满分.等这周结束后,找高人询问下trick. 若 ...

  3. (转载)图片左右滚动控件(带倒影)——重写Gallery

    今天在网上找了些资料,做了一个图片左右滚动的Demo,类似幻灯片播放,同时,图片带倒影效果,运行效果如下图: 实现方式是重写Gallery,使用自定义的Gallery来实现这一效果,工程一共三个文件, ...

  4. Java 反射 方法调用

    在使用Java 反射时,对方法的调用,可能碰到最多的问题是,方法的变量如何使用.其实,调用方法的变量全部在参数数组里,不管有多少个参数,你都要把它放在参数数组里,如果是单个非数组参数,则可不使用参数数 ...

  5. 网络直播电视之M3U8解析篇 (下)

    在上一篇文章中讲述了网络直播电视的M3U8解析和当中的keyword段.本章我将对我遇见到的不同数据源的M3U8文件进行列举和分析. 第一种:ts片段地址为文件名,下载地址为:http:\\www.X ...

  6. Android高级图片滚动控件,编写3D版的图片轮播器

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/17482089 大家好,好久不见了,最近由于工作特别繁忙,已经有一个多月的时间没写博 ...

  7. WebApi2官网学习记录---Media Formatters

    Web API内建支持XML.JSON.BSON.和form-urlencoded的MiME type. 创建的自定义MIME类型要继承一下类中的一个: MediaTypeFormatter 这个类使 ...

  8. 网站全局js代码

    这几天开始看公司的一套系统,整理的网站全局js代码 /*文件名:base.js功能说明:全站通用的全局变量及公用方法创建日期:2010-09-26*///引入jquery库文件document.wri ...

  9. Windows命令行(DOS命令)教程-7 (转载)http://arch.pconline.com.cn//pcedu/rookie/basic/10111/15325_6.html

    11. deltree [功能] 删除目录树 [格式] [C:][path]DELTREE [C1:][path1] [[C2:][path2] […]] [说明] 这个命令将整个指定目录树全部消灭, ...

  10. 读取并解析properties文件

    public class SysConfig { private static final Properties properties = new Properties(); static{ Reso ...