LeetCode 【46. Permutations】
Given a collection of distinct 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],
[3,2,1]
] 思路:
首先,固定第一个数字,递归求后序数组的全排列,比如example中,递归过程为:固定1,求[2,3]的全排列,然后固定2,求[3]的全排列,固定[3]求,空数组的全排列,由于此时数组为空,所以将结果保存,返回。
然后交换位子,继续进行递归,说的有点不是很明白,看一张图就比较清晰了。如图是一颗枚举树。现在的问题是怎么出现1,2,3开头的节点呢?就是使用交换,比如1和2交换,得到[2,1,3]固定2,求[1,3]的全排列。
那交换以后怎么得到3开头的呢?由于是递归,所以可以交换回1,2然后再交换1,3得到3开头的。

代码
class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> result;
permuteRec( 0, nums, result);
return result;
}
private:
void permuteRec( int start, vector<int> &nums, vector<vector<int>> &result ){
if( start >= nums.size()){
result.push_back(nums);
return;
}
for( int i = start; i < nums.size(); i++ ){
swap( nums[start], nums[i] );
permuteRec( start+1, nums, result);
swap( nums[start], nums[i] );
}
}
};
LeetCode 【46. Permutations】的更多相关文章
- [Leetcode][Python]46: Permutations
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 46: Permutationshttps://leetcode.com/pr ...
- LeetCode:46. Permutations(Medium)
1. 原题链接 https://leetcode.com/problems/permutations/description/ 2. 题目要求 给定一个整型数组nums,数组中的数字互不相同,返回该数 ...
- LeetCode 【47. Permutations II】
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【线段树】【4-6组队赛】Problem H
Problem Description #include <iostream> #include <algorithm> using namespace std; int n, ...
- LeetCode【第一题】Two Sum
准备刷一刷LeetCode了. 题目: ''' Given an array of integers, return indices of the two numbers such that they ...
- 知乎上的一些文章---leetcode【笔记1】
张土汪 http://github.com/shawnfan Java{script}代码仔 42 人赞同 [1.19.2017] 更新: 2017年1月17日, 陪我征战多年的 2014 MackB ...
- leetcode【14题】Longest Common Prefix
题目:Longest Common Prefix 内容: Write a function to find the longest common prefix string amongst an ar ...
- LeetCode【110. 平衡二叉树】
对于平衡二叉树,就是左右深度相差1 就可以另外弄一个函数,计算深度,然后, 在原函数上进行比较深度是否相差1,再输出true or false. 至于迭代就可以,比较完左右节点,再比较各自的左右节点. ...
- leetcode 【Rotate List 】python 实现
题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example:Giv ...
随机推荐
- svn server
svn server: 1.c:\Program Files\TortoiseSVN\bin>svnserve -d -r C:\Jasper\Repositories2.change the ...
- redis 库相关命令
切换数据库: select 1 查看数据库大小:dbsize 清空数据库: flushdb
- swift混编oc碰到的问题
在swift中混编苹果官方的Reachability OC文件. 因为swift工程的target是生成framework而非app,framework中调用oc与app中使用桥接文件还不一样,参考: ...
- flash的动态加载技术
这里所说的动态加载技术, 主要是指代码模块(可以是swc也可以是swf)的动态加载.即主swf在运行的时候, 可以根据需要动态加载所需的代码模块. 为了讨论方便, 下面所说的代码模块都用swc表示,用 ...
- html知识——表单
1.表单标记:<form>内容</form> 属性: name(表单名称) 必须写!!! method(传送数据方式): get--将表单的内容附加在URL地址的后面 ...
- json 解析 真是一篇让我泪流满面的好文章
http://my.eoe.cn/iceskysl/archive/19629.html点击打开链接
- zoj 1789 The Suspects
好高兴,又AC一道 ,不过是很类似的两道..还是好高兴呀思想跟2833是一样的,不过要重新设计输入和输出.老师上课又重新讲解了一下,因为嫌疑人已知是0,所以加入集中时应该默认让数值小的做树根,即最终让 ...
- 转Global.asax文件
Global.asax 文件是什么 Global.asax 文件,有时候叫做 ASP.NET 应用程序文件,提供了一种在一个中心位置响应应用程序级或模块级事件的方法.你可以使用这个文件实现应用程序 ...
- python中列表的常用方法
s=[1,2,3] s[3]=12#列表长度小于3时无法给列表赋值 len(s)#列表长 s+s s*5#l列表重复5次 5 in s#判断元素是否在列表中,返回true or false max(s ...
- Struts2+Hibernate+Spring 整合示例[转]
原文 http://blog.csdn.net/tkd03072010/article/details/7468769 Spring整合Struts2.Hibernate原理概述: 从用户角度来看,用 ...