Permutations
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]
]
分析: 全排列实现思想是从start开始,分别和后面的数进行交换,递归求解
class Solution {
public:
void swap(int i, int j, vector<int>& nums){
int temp =nums[i];
nums[i] = nums[j];
nums[j]= temp;
}
void allRange(int start, vector<int>& nums, vector<vector<int>>& res)
{
if(start==nums.size()-1)
return;
for(int i =start; i<nums.size(); i++){
cout << start << " "<<i<<endl;
swap(start, i, nums);
if(start!=i)
res.push_back(nums);
allRange(start+1, nums, res);
swap(i, start, nums);
}
}
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> res;
if(nums.size()==0)
return res;
res.push_back(nums);
allRange(0, nums, res);
return res;
}
};
Permutations的更多相关文章
- 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 ...
- 【leetcode】Permutations
题目描述: Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the ...
- [leetcode] 47. 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 ...
- one recursive approach for 3, hdu 1016 (with an improved version) , permutations, N-Queens puzzle 分类: hdoj 2015-07-19 16:49 86人阅读 评论(0) 收藏
one recursive approach to solve hdu 1016, list all permutations, solve N-Queens puzzle. reference: t ...
- 46. Permutations 回溯算法
https://leetcode.com/problems/permutations/ 求数列的所有排列组合.思路很清晰,将后面每一个元素依次同第一个元素交换,然后递归求接下来的(n-1)个元素的全排 ...
随机推荐
- Kerberos简介
Kerberos协议: Kerberos协议主要用于计算机网络的身份鉴别(Authentication), 其特点是用户只需输入一次身份验证信息就可以凭借此验证获得的票据(ticket-grantin ...
- mysql errno 150
mysql error Number 1005can't creat table'/test/#sql-640_1.frm'(errno:150)三种可能问题 外键和被引用外键类型不一样,比如inte ...
- java静态方法调用&&构造函数&&静态块
静态方法,也就是使用static声明的方法,在虚拟机启动加载类的时候就进行了创建,所以使用到静态方法时,直接使用类名点静态方法即可调用.java在执行静态方法前,不会调用构造函数:构造函数是在实例化j ...
- table tr foreach td 换行
@{ ;} <table style=" class="sy_table"> <tr> @foreach (DataRow dr in (View ...
- 一些有用的SAP技术TCODE
Background Processing RZ01 Job Scheduling Monitor SM36 Schedule Background Job SM36WIZ Job definitio ...
- 理解 iOS 的内存管理
远古时代的故事 那些经历过手工管理内存(MRC)时代的人们,一定对 iOS 开发中的内存管理记忆犹新.那个时候大约是 2010 年,国内 iOS 开发刚刚兴起,tinyfool 大叔的大名已经如雷贯耳 ...
- ios 显示缓存数据库
IOS8以前版本, 如果是模拟器的话, 在这个路径中找到你的应用:/Users/你的用户名/Library/Application Support/iPhone Simulator/5.1/Appli ...
- iOS 本地推送通知
1.什么是本地推送通知 不需要联网的情况下,应用程序经由系统发出的通知 2.本地推送的使用场景 定时提醒,如玩游戏.记账.闹钟.备忘录等 3.实现本地推送通知的步骤 创建本地推送通知的对象UILoca ...
- Hadoop学习2
搭建伪分布式完成之后: 伪分布式安装详细介绍:http://www.powerxing.com/install-hadoop/ 练习1 编写Java程序实现以下函数: 1.向HDFS中上传文件 2.从 ...
- regsvr32命令
regsvr32是Windows操作系统命令,用来注册及反注册DLL文件和ActiveX文件. 1. 使用示例 regsvr32 foo.dll // 注册foo.dll文件到Windows ...