1. Given a collection of numbers, return all possible permutations.
  2.  
  3. For example,
  4. [1,2,3] have the following permutations:
  5. [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,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] == false)
  12. {
  13. temp[size] = num[i];
  14. flag[i] = true;
  15. DFS(num, size+, temp);
  16. flag[i] = false;
  17. }
  18. }
  19. }
  20. vector<vector<int> > permute(vector<int> &num) {
  21. // Start typing your C/C++ solution below
  22. // DO NOT write int main() function
  23. n = num.size();
  24. result.clear();
  25. flag.resize(n,false);
  26.  
  27. if(n == ) return result;
  28. vector<int> temp(n,) ;
  29. DFS(num,,temp);
  30.  
  31. return result ;
  32. }
  33. private :
  34. int n;
  35. vector<bool> flag;
  36. vector<vector<int>> result;
  37. };

LeetCode_Permutations的更多相关文章

  1. LeetCode_Permutations II

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

随机推荐

  1. Powershell使用管道

    管道并不是什么新事物,以前的Cmd控制台也有重定向的命令,例如Dir | More可以将结果分屏显示.传统的Cmd管道是基于文本的,但是Powershell是基于对象. PS> ls | Sor ...

  2. C#读取文件高效方法实现

     C# Code  12345678910111213141516171819202122232425262728293031           private void button1_Click ...

  3. MySQL数学函数

    官方文档:Numeric Functions and Operators Name Description ABS() Return the absolute value ACOS() Return ...

  4. Linux开发工具的使用

    1.   Linux开发工具的使用 Vim编译的使用 Gdb调试工具的使用 Makefile的编写 linux跟踪调试 SSH的使用 subversion的使用 1.   Linux开发工具的使用 V ...

  5. ASP.NET文件组成(转载于Owen的BLOG)

    一.扩展名: .aspx:窗体文件,为前台程序. .cs文件:类文件,主要为后台数据处理,供所有的.aspx文件的后台应用. .asmx文件:用于创建从其他应用程序使用的web服务的类. .css文件 ...

  6. 什么是 docker?

    关于 Docker 是什么,有个著名的隐喻:集装箱.但是它却起了个“码头工人”( docker 的英文翻译)的名字.这无疑给使用者很多暗示:“快来用吧!用了 Docker ,就像世界出现了集装箱,这样 ...

  7. 5 c语言数组

    其中包括:冒泡 高精度加法 统计不相同的数字个数(数组) 数组元素倒序输出 go~~~~ #include <stdio.h> /* 功能:冒泡 时间:2016.11.15 */ void ...

  8. poj 3273 Monthly Expense(二分搜索之最大化最小值)

    Description Farmer John ≤ moneyi ≤ ,) that he will need to spend each day over the next N ( ≤ N ≤ ,) ...

  9. 执行testng appium用例失败,自动截图

    新建一个截图监听类ScreenShotListener ,重写onTestFailure方法,里面定义了 监听的driver ,截图文件路径和名称 package com.fsssc.htsgl.ut ...

  10. javascript对象的理解

    从代码中体会javascript中的对象: <!DOCTYPE html> <html> <head> <meta charset="utf-8&q ...