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的更多相关文章

  1. leetcode46. Permutations 、47. Permutations II、 剑指offer字符串的排列

    字符串排列和PermutationsII差不多 Permutations第一种解法: 这种方法从0开始遍历,通过visited来存储是否被访问到,level代表每次已经存储了多少个数字 class S ...

  2. Leetcode46. Permutations全排列

    给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1 ...

  3. LeetCode46,47 Permutations, Permutations II

    题目: LeetCode46 I Given a collection of distinct numbers, return all possible permutations. (Medium) ...

  4. [Swift]LeetCode46. 全排列 | Permutations

    Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...

  5. Permutations II

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

  6. [LeetCode] Permutations II 全排列之二

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

  7. [LeetCode] Permutations 全排列

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  8. POJ2369 Permutations(置换的周期)

    链接:http://poj.org/problem?id=2369 Permutations Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  9. Permutations

    Permutations Given a collection of distinct numbers, return all possible permutations. For example,[ ...

随机推荐

  1. My first Python program(附增加清屏方法)

    #TempConvert.py TempStr = input("请输入带有符号的温度值:") if TempStr[-1] in ['F', 'f']: C = (eval(Te ...

  2. URAL 1145—— Rope in the Labyrinth——————【求树的直径】

    Rope in the Labyrinth Time Limit:500MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64 ...

  3. 前端性能优化-keep-alive

    什么是Keep-Alive Keep-Alive是浏览器端和服务器端约定的一种提高传输效率的协议.我先举个例子吧,我现在搬家,有10个箱子,如果我自己来搬的话,每次只能带一个箱子,那么搬到目的地,需要 ...

  4. POS开发问题 - 输入非数字弹出提示框的实现

    业务场景: 一个输入框,如果输入非数字,那么弹出提示框,如下图 点击确定,输入框自动清空非数字的输入,并且自动获得焦点,如图: 实现方案: 实现的想法: 给输入框添加一个 input 事件,给输入框绑 ...

  5. Native Method

    While a 100% pure Java solution is nice in principle, realistically, for an application, there are s ...

  6. Oracle数据表比较记录差异(转)

    liuyx_know|七级 你可以参照一下Oracle的UNION [ALL], INTERSECT, MINUS操作符,至于你的问题你可以使用MINUS操作符,语句如下: SELECT * FROM ...

  7. typedef struct 与 struct

    学c++之前最好先学c.特别要说的是,一些虽然冠名为c++的项目的文件中却大部分都是c的代码. 比如我们这个例子: 在c语言中,定义一个结构体和其实适合c++中有区别的.比如我们有如下的代码: str ...

  8. 拼接sql语句时拼接空字符串报sql错误

    先上代码(php): $id_card=""; $sql = "select * from people where id_card=".$id_card; 看 ...

  9. QR分解与最小二乘(转载自AndyJee)

    转载网址:http://www.cnblogs.com/AndyJee/p/3846455.html 主要内容: 1.QR分解定义 2.QR分解求法 3.QR分解与最小二乘 4.Matlab实现 一. ...

  10. IOS 数据加密总结(及MD5加密)

    数据安全总结 1.网络数据加密1> 加密对象:隐私数据,比如密码.银行信息2> 加密方案* 提交隐私数据,必须用POST请求* 使用加密算法对隐私数据进行加密,比如MD53> 加密增 ...