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)个元素的全排 ...
随机推荐
- AlloyRenderingEngine开门大吉
快速入口 不读文章可以直接拐向这里: github:https://github.com/AlloyTeam/AlloyRenderingEngine website:http://alloyteam ...
- iOS 文件下载
iOS 视频音乐类等应用会用到“文件下载”.文件下载在iOS中的实现如下: 1.小文件下载 @interface ViewController () <NSURLConnectionDataDe ...
- Sharepoint学习笔记—习题系列--70-576习题解析 -(Q135-Q137)
Question 135 You work for a software company that sells Web Parts to customers. You designed the fi ...
- 使用xhprof分析php代码性能
推荐在Linux平台使用xhprof,win下xhprof目前稳定版本在php5.5 安装xhprof 下载地址 http://pecl.php.net/get/xhprof-0.9.4.tgz 与p ...
- Ubuntu下安装中文输入法
搜狗输入法 for Linux 是基于Fcitx 框架(fcitx-sogoupinyin). 安装环境为Ubuntu 13.04 安装过程: 卸载Ubuntu默认的ibus输入法: sudo apt ...
- RMAN异机恢复遭遇ORA-01547、ORA-01152、ORA-01110错误案例
测试环境: 操作系统 : Red Hat Enterprise Linux ES release 4 (Nahant Update 4) VMWARE 数据库 : O ...
- winform窗体(一)——基本属性
一.窗体设计界面 二.部分属性 1.基本 设计中的Name:窗体类的类名 AcceptButton:窗口的确定按钮Enter CancelButton:窗口按ESC的取消按钮 2.外观 Backcol ...
- SQL*LOADER错误总结
在使用SQL*LOADER装载数据时,由于平面文件的多样化和数据格式问题总会遇到形形色色的一些小问题,下面是工作中累积.整理记录的遇到的一些形形色色错误.希望能对大家有些用处.(今天突然看到自己以前整 ...
- html中的meta详解
1 name=viewport <meta name="viewport" content="width=device-width,initial-scale=1 ...
- hadoop从非HA转到NAMENODE HA时需要注意的一个问题
配置core-site.xml 配置hdfs-site.xml 配置mapred-site.xml 配置yarn-site.xml 纷发至其他节点 修改RM 2 ..N 上面的节点信息 格式化ZK h ...