LeetCode 046 Permutations
题目要求:Permutations(全排列)
Given a collection of 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], and [3,2,1].
分析:
参考网址:http://www.cnblogs.com/panda_lin/archive/2013/11/12/permutations.html
可以使用STL的next_permutation函数。
不使用的话,要递归生成全排列。
① 生成[2, 3]的全排列[2, 3]和[3, 2],然后把1加上去生成[1, 2, 3]和[1, 3, 2]。
② 交换1和2的位置,生成[1, 3]的全排列[1, 3]和[3, 1],然后把2加上去生成[2, 1, 3]和[2, 3, 1]。
③在第二步的基础上交换2和3的位置,生成[2, 1]的全排列[2, 1]和[1, 2],然后把3加上去生成[3, 2, 1]和[3, 1, 2]。
① [1, 2, 3] [1, 3, 2]
② [2, 1, 3] [2, 3, 1]
③ [3, 2, 1] [3, 1, 2]
代码如下:
class Solution {
public:
vector<vector<int> > permute(vector<int> &num) {
vector<vector<int>> result;
sort(num.begin(), num.end());
//STL next_permutation
do{
result.push_back(num);
}while(next_permutation(num.begin(), num.end()));
return result;
}
};
class Solution {
public:
void internalPermute(vector<int> &num, int index, vector<int> &perm, vector<vector<int> > &result) {
int size = num.size();
if (size == index) {
result.push_back(perm);
}
else {
for (int i = index; i < size; ++i) {
swap(num[index], num[i]);
perm.push_back(num[index]);
internalPermute(num, index + 1, perm, result);
perm.pop_back();
swap(num[index], num[i]);
}
}
}
vector<vector<int> > permute(vector<int> &num) {
vector<vector<int> > result;
vector<int> perm;
internalPermute(num, 0, perm, result);
return result;
}
};
LeetCode 046 Permutations的更多相关文章
- Java for LeetCode 046 Permutations
Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the follo ...
- LeetCode 046 Permutations 全排列
Given a collection of distinct numbers, return all possible permutations.For example,[1,2,3] have th ...
- Java for LeetCode 047 Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- [LeetCode] 47. Permutations II 全排列 II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【LeetCode】046. Permutations
题目: Given a collection of distinct numbers, return all possible permutations. For example,[1,2,3] ha ...
- LeetCode 46 Permutations(全排列问题)
题目链接:https://leetcode.com/problems/permutations/?tab=Description Problem:给出一个数组(数组中的元素均不相同),求出这个数组 ...
- 046 Permutations 全排列
给定一个含有不同数字的集合,返回所有可能的全排列.比如,[1,2,3] 具有如下排列:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2 ...
随机推荐
- 数据结构 - 二叉树的遍历(递归VS非递归)
import java.util.LinkedList; public class BinaryTree { public static void main(String[] args) { int ...
- MobaXterm 连接 VirtualBox 6 虚拟机中的 CentOS 7
1 运行环境 本机系统:Windows 7 虚拟机软件:Oracle VM VirtualBox 6 虚拟机系统:CentOS 7 MobaXterm(安装在本机上) 2 MobaXterm - 远端 ...
- nginx&http 第四章 ngx http代理 upstream流程图
- linux nf_conntrack 连接跟踪机制 3-hook
conntrack hook函数分析 enum nf_ip_hook_priorities { NF_IP_PRI_FIRST = INT_MIN, NF_IP_PRI_CONNTRACK_DEFRA ...
- binary hacks读数笔记(堆、栈 VMA的分布)
一.首先看一个简单的程序: #include<stdlib.h> int main() { while(1) { sleep(1000); } return 0; } gcc -stati ...
- laravel5.5 如何创建Facades并使用
laravel Facades概念略,自行百度. 如何创建Facades使用步骤1. 创建一个php文件,App\Utils\SmsSend.php.<?php namespace App\Ut ...
- HotSpot类模型之InstanceKlass
上一篇 HotSpot源码分析之类模型 介绍了类模型的基础类Klass的重要属性及方法,这一篇介绍一下InstanceKlass及InstanceKlass的子类. 1.InstanceKlass类 ...
- Linxu (centos6.8)常见目录及文件解析
/etc/sysconfig/networ-scripts/ifcfg-eth0 第一块网卡配置 BOOTPROTO="dhcp" #启用地址协议,static静态协议,bo ...
- 原生javascript包装一个ajax方法
调用AJAX 1 <script type="text/javascript" src="ajax.js"></script> 2 &l ...
- python-网络安全编程第五天(爬虫模块BeautifulSoup)
前言 昨晚学的有点晚 睡得很晚了,今天早上10点多起来吃完饭看了会电视剧就瞌睡了一直睡到12.50多起来洗漱给我弟去开家长会 开到快4点多才回家.耽搁了不少学习时间,现在就把今天所学的内容总结下吧. ...