LeetCode46. Permutations
Given a collection of distinct integers, return all possible permutations.
Example:
Input: [1,2,3]
Output:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
法1.回溯法。递归。每次交换num中的两个数字。第一个数字固定,对后面的数字进行全排列。输出所有全排列数字之后,还原最初的num。再重复第一步:交换第一个数字和后面的数字。
细节:结束条件start==num.size()。每次交换后的num在输出所有全排列之后要还原到最初的num。

class Solution {
public:
vector<vector<int>> permute(vector<int> &num) {
vector<vector<int>> ret;
Helper(num,ret,0);
return ret;
}
void Helper(vector<int> num,vector<vector<int>> & ret,int start)
{
if(start==num.size())
{
//一种全排列
ret.push_back(num);
}
for(int i = start ; i<num.size() ; i++)
{
swap(num[i],num[start]);//交换当前
Helper(num,ret,start+1);//进入下一层布局(后部分全排列)
swap(num[i],num[start]);//回到上一层布局
}
}
};
法2.递归。回溯。申请一个空数组out,长度为num大小。从out的第一个空位置开始,在num中选一个数填入out。用数组visited来表示num的元素是否访问过。一直到递归到index=size的时候,打印。每次打印完之后,要回溯到上一位,并且visited恢复为未访问。
class Solution {
public:
vector<vector<int>> permute(vector<int> &num) {
vector<vector<int>> ret;
if(num.size()==0) return ret;
vector<int> out,visited(num.size(),0);
Helper(num,out,ret,0,visited);
return ret;
}
void Helper(vector<int> num,vector<int> & out,
vector<vector<int>> &ret,int &index,vector<int> &visited)//int index不能用引用,他是const
{
if(index == num.size())
{
//一次排列完成
ret.push_back(out);
return;
}
for(int i =0 ;i<num.size();i++)
{
if(visited[i]==1)
continue;
visited[i]=1;
out.push_back(num[i]);
Helper(num,out,ret,index+1,visited);
out.pop_back();
visited[i]=0;
}
}
};
细节。 关于index的形参定义:不能用左值引用!因为无法传递常数进去,常数是无法更改的!
void Helper(vector<int> num,vector<int> & out,vector<vector<int>> &ret,int &level,vector<int> &visited);
//定义错误 形参不能设置为左值引用。
Helper(num,out,ret,0,visited);
cannot bind non-const lvalue reference of type 'int&' to an rvalue of type 'int'
0作为常量,只能做右值。所以不能用非常量左值引用作为形参。
LeetCode46. Permutations的更多相关文章
- leetcode46. Permutations 、47. Permutations II、 剑指offer字符串的排列
字符串排列和PermutationsII差不多 Permutations第一种解法: 这种方法从0开始遍历,通过visited来存储是否被访问到,level代表每次已经存储了多少个数字 class S ...
- Leetcode46. Permutations全排列
给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1 ...
- LeetCode46,47 Permutations, Permutations II
题目: LeetCode46 I Given a collection of distinct numbers, return all possible permutations. (Medium) ...
- [Swift]LeetCode46. 全排列 | Permutations
Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...
- Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] Permutations 全排列
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- POJ2369 Permutations(置换的周期)
链接:http://poj.org/problem?id=2369 Permutations Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- Permutations
Permutations Given a collection of distinct numbers, return all possible permutations. For example,[ ...
随机推荐
- Beam概念学习系列之PCollection数据集
不多说,直接上干货! PCollection数据集 PCollection是Apache Beam中数据的不可变集合,可以是有限的数据集合也可以是无限的数据集合. 有限数据集,这种一般对应的是批处理 ...
- linux下追查线上问题常用命令
(1)查占用cpu最多的进程方法一:核心指令:ps实际命令:ps H -eo pid,pcpu | sort -nk2 | tail执行效果如下:[work@test01 ~]$ ps H -eo p ...
- DB2错误码大全
sqlcode sqlstate 说明 000 00000 SQL语句成功完成 01xxx SQL语句成功完成,但是有警告 +012 01545 未限定的列名被解释为一个有相互关系的引用 +098 0 ...
- 为什么地址栏的快捷键是Alt D
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:为什么地址栏的快捷键是Alt D.
- Snipaste的详细安装和使用
Snipaste安装和使用 1:snipaste的安装 步骤一: https://zh.snipaste.com/ ,去此官网下载. 步骤二:由于此是个绿色软件,直接解压即可. 步骤三:使用,见官网 ...
- maven课程 项目管理利器-maven 3-2 maven自动建立目录骨架
使用cmd创建maven目录的两种方式: 使用archetype插件 1 按照提示进行选择 步骤: a 进入指定目录 b mvn archetype:generate --创建项目目录 c ente ...
- 减少服务器压力php生成静态xml文件
一.引 言 在速度上,静态页面要比动态页面的比方php快很多,这是毫无疑问的,但是由于静态页面的灵活性较差,如果不借助数据库或其他的设备保存相关信息的话,整体的管理上比较繁琐,比方修改编辑.比方阅读权 ...
- 安全漏洞 : XSS CSRF
https://my.oschina.net/hc24/blog/527099 XSS成因概括 : XSS其实就是Html的注入问题,攻击者A的输入没有经过严格的控制进入了数据库,最终显示给来访的用户 ...
- OC跟Swift混编
OC项目中使用Swift 本文版权归作者所有,如需转载请联系孟祥月 CSDN博客:http://blog.csdn.net/mengxiangyue 独立博客:http://mengxiangyue ...
- 零基础逆向工程40_Win32_14_枚举窗口_模拟鼠标键盘
1 查找窗口 1.1 代码案例 //查找指定窗口 TCHAR szTitle[MAX_PATH] = {0}; HWND hwnd = ::FindWindow(TEXT("#32770&q ...