LeetCode_Permutations
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的更多相关文章
- LeetCode_Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
随机推荐
- MySQL常用聚合函数
官方文档:Aggregate (GROUP BY) Functions Name Description AVG() Return the average value of the argument ...
- Manacher算法----最长回文子串
题目描述 给定一个字符串,求它的最长回文子串的长度. 分析与解法 最容易想到的办法是枚举所有的子串,分别判断其是否为回文.这个思路初看起来是正确的,但却做了很多无用功,如果一个长的子串包含另一个短一些 ...
- cocos2dx-lua绑定自定义c++类(一)
本文主要介绍mac上,如何将自定义的c++类,绑定到lua. 1.工具先行 找到 你的cocos2d-x/tools/tolua++,里面文件按类型大致分为: (1)*.pkg:用于定义要绑定的c++ ...
- Android单元测试: 首先,从是什么开始
Android单元测试: 首先,从是什么开始 http://chriszou.com/2016/04/13/android-unit-testing-start-from-what.html 这是一系 ...
- C语言中所有变量和常量所使用的内存总结
(1)相同点:三种获取内存的方法,都可以给程序提供可用内存,都可以用来定义变量给程序用.(2)不同点:栈内存对应C中的普通局部变量(别的变量还用不了栈,而且栈是自动的,由编译器和运行时环境共同来提供服 ...
- java之File
1.文件创建.重命名.删除 code: package com.test; import java.io.File; import java.io.IOException; public class ...
- Beyond Compare 忽略两个文件内容的顺序比较文件内容(xjl456852原创)
有时两个文件内容的顺序是不固定的,对比时需要忽略文件顺序进行对比. 可以这样设置: 点击菜单下面工具栏按钮: 点击Format旁的三角,选择Sorted,就会按文件的顺序排序比较.忽略了文件内容顺序的 ...
- Sonar入门(五):使用 Sonar 进行代码质量管理
Sonar 概述 Sonar 是一个用于代码质量管理的开放平台.通过插件机制,Sonar 可以集成不同的测试工具,代码分析工具,以及持续集成工具. 与持续集成工具(例如 Hudson/Jenkins ...
- this关键字的解析
this关键字的作用: 1.表示类中的属性. class Person{ // 定义Person类 private String name ; // 姓名 private int age ; // 年 ...
- [Redux] Extracting Presentational Components -- AddTodo
The code to be refactored: let nextTodoId = 0; class TodoApp extends Component { render() { const { ...