题目要求: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的更多相关文章

  1. Java for LeetCode 046 Permutations

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

  2. LeetCode 046 Permutations 全排列

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

  3. Java for LeetCode 047 Permutations II

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

  4. 【LeetCode】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

  5. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  6. [LeetCode] 47. Permutations II 全排列 II

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

  7. 【LeetCode】046. Permutations

    题目: Given a collection of distinct numbers, return all possible permutations. For example,[1,2,3] ha ...

  8. LeetCode 46 Permutations(全排列问题)

    题目链接:https://leetcode.com/problems/permutations/?tab=Description   Problem:给出一个数组(数组中的元素均不相同),求出这个数组 ...

  9. 046 Permutations 全排列

    给定一个含有不同数字的集合,返回所有可能的全排列.比如,[1,2,3] 具有如下排列:[  [1,2,3],  [1,3,2],  [2,1,3],  [2,3,1],  [3,1,2],  [3,2 ...

随机推荐

  1. Java学习的第七天

    1.今天学了数组 数组名称.length是数组的长度. 数组打印 二维数组: 多维数组同二维数组类似. 最大值最小值算法. 2.在输入过程中的 定义 int 名=con.nextInt();当时不懂. ...

  2. python爬虫scrapy框架

    Scrapy 框架 关注公众号"轻松学编程"了解更多. 一.简介 Scrapy是用纯Python实现一个为了爬取网站数据.提取结构性数据而编写的应用框架,用途非常广泛. 框架的力量 ...

  3. 数据库本地data source

    数据源表示数据库所在设备的ip地址或名称,基本上有以下几种写方法 data source = (local) data source = "127.0.0.1" data sour ...

  4. Fira Code字体安装与配置

    俗话说,工欲善其事,必先利其器.算法固然重要,但真正实践也很重要. 一个字体的好看程度,直接决定了写代码和看代码的心情.比如这样: 代码1: #include <iostream> #in ...

  5. python开发初识(一)

    python开发 机器码和字节码 机器码 :计算机可以直接认识的语言 字节码 :高级语言转换成机器码去执行 语言之间的对比: C,汇编 :C语言是根语言 python Java :既能写前端,又能写后 ...

  6. leetcode64:maximal-rectangle

    题目描述 给出一个只包含0和1的二维矩阵,找出最大的全部元素都是1的长方形区域,返回该区域的面积. Given a 2D binary matrix filled with 0's and 1's, ...

  7. 3.2spring源码系列----循环依赖源码分析

    首先,我们在3.1 spring5源码系列--循环依赖 之 手写代码模拟spring循环依赖 中手写了循环依赖的实现. 这个实现就是模拟的spring的循环依赖. 目的是为了更容易理解spring源码 ...

  8. Java学习之路(一)——JDK的下载与安装

    (一).JDK是Java开发工具包 下载网址:https://www.oracle.com/java/technologies/javase-downloads.html (二).安装 下载JDK以后 ...

  9. CSS兼容性总结一点点

    CSS3的兼容性,除了前缀.还有参数格式的区分,因为仍在变化中,不在这篇文章中讨论. 很想总结一下IE 6 7 8 9 10的兼容性问题,但是我实在不喜欢IE 6 7 8,在Web开发上也很少再调整到 ...

  10. JS超酷时钟的制作

    通过补充代码,实现时钟实时显示当前时间:年.月.日.时.分.秒.日期. <!DOCTYPE html> <html> <head lang="zh-CN&quo ...