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,[ ...
随机推荐
- c++ 和 matlab 下的caffe模型输入差异
在向一个caffe模型传递输入数据的时候,要注意以下两点: 1. opencv中Mat数据在内存中的存放方式是按行存储,matlab中图像在内存中的存放方式是按列存储. 2. opencv中Mat数据 ...
- 几种常见的排序算法Java实现总结
public class MySort { final int MAX=20; int num[]=new int[MAX]; { System.out.print("生成的随机数组是:&q ...
- (转)Shell脚本编程--Uniq命令
uniq 原文:http://blog.csdn.net/xifeijian/article/details/9209627 uniq命令可以去除排序过的文件中的重复行,因此uniq经常和sort合用 ...
- [PHP]Mysql的运用
1.创建数据库和表,代码如下: //创建数据库函数 function createDB($DBname){ $query="CREATE DATABASE $DBname"; re ...
- pat1043. Is It a Binary Search Tree (25)
1043. Is It a Binary Search Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
- pat1040. Longest Symmetric String (25)
1040. Longest Symmetric String (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, ...
- MS-DOS
MS-DOS doskey /history /reinstall /buffersize /macros doskey di=dir /w/p defrag 磁盘碎片整理 xcopy deltree ...
- c#实体转化
经常会遇到把一个实体转化成另一个实体这样的情况,实体的属性一个一个手写去转化不反对,但不是啥好的方法:可以使用反射写一个通用的实体转化类,针对任何实体转化,不用再去自己手写. public stati ...
- spring 类注入失败,解决之道
1.今天偶尔发现的问题,如果你在一个类上面用了注解@Async,spring的异步注解之后,发现如果别的类用@Autowired导入这个类时会失败! 解决办法:用了@Async无非是想方便的用异步操作 ...
- .Net Core 应用框架。
1.分布式系统框架 https://github.com/MassTransit/MassTransit 2.搜索引擎 https://github.com/XiLife-OSPC/Masuit. ...