Given a collection of numbers, return all possible permutations.

For example,
[1,2,3] have the following permutations:
[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,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] == false)
{
temp[size] = num[i];
flag[i] = true;
DFS(num, size+, temp);
flag[i] = false;
}
}
}
vector<vector<int> > permute(vector<int> &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
n = num.size();
result.clear();
flag.resize(n,false); if(n == ) return result;
vector<int> temp(n,) ;
DFS(num,,temp); return result ;
}
private :
int n;
vector<bool> flag;
vector<vector<int>> result;
};

LeetCode_Permutations的更多相关文章

  1. LeetCode_Permutations II

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

随机推荐

  1. 自制单片机之一------AT89S51最小系统制做

    C51最小系统电路在网上一搜一大把,大同小异.我略做改动后如图: 加一个11.0592MHZ的晶振是为了以后做串口通信时和PC有相同的波特率.可用短路帽切换.说说板子的布局:网上卖的最小系统都是把板子 ...

  2. KEIL 伪指令

    //为了大家查找方便,命令按字母排序:0.ALTNAME 功能: 这一伪指令用来自定义名字,以替换源程序中原来的保留字,替换的保留字均可等效地用于子程序中. 格式: ALTNAME 保留字 自定义名 ...

  3. 终于懂了:Delphi的函数名不是地址,取地址必须遵守Object Pascal的语法(Delphi和C的类比:指针、字符串、函数指针、内存分配等)good

    这点是与C语言不一样的地方,以前我一直都没有明白这一点,所以总是不明白:函数地址再取地址算怎么回事? ------------------------------------------------- ...

  4. 关于Python的3张图

  5. expect 传参

    AAAAAAAAA(A)/app/cbsrun/sbin> cat reloadtuxconfig.exp puts "Start" set i 1 set max_i [l ...

  6. auto space advisor

    <pre name="code" class="sql">首先:oracle有自动Job,进行shrink space SQL> select ...

  7. 把第三方的exe程序嵌入C#界面上

    public partial class eTerm_Form : WinFormsUI.Docking.DockContent{public eTerm_Form(){InitializeCompo ...

  8. bzoj2965

    http://www.lydsy.com/JudgeOnline/problem.php?id=2965 http://www.tsinsen.com/A1385 平面图网络流. 首先我们要将平面图转 ...

  9. mysql 获取当前时间戳

      mysql 获取当前时间为select now() 运行结果: 2012-09-05 17:24:15 mysql 获取当前时间戳为select unix_timestamp(now()) 运行结 ...

  10. JavaScript 数组的遍历

    var a = [1, 2, 3]; // for循环 for(var i = 0; i < a.length; i++) { console.log(a[i]); } // while循环 v ...