题目要求: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. C语言经典100例-ex001

    系列文章<C语言经典100例>持续创作中,欢迎大家的关注和支持. 喜欢的同学记得点赞.转发.收藏哦- 后续C语言经典100例将会以pdf和代码的形式发放到公众号 欢迎关注:计算广告生态 即 ...

  2. svg究竟是什么?

    svg究竟是什么? 1 要点 要点1:svg与jpg/png等格式的用途完全不同,不可相提并论,没有可比性,不可互相替代. 要点2:日常生活中,我们用相机拍摄自然景象得到的照片和视频,能且只能用jpg ...

  3. SQL service 数据插入

    目的:实现对数据库XDSA中表S72.C72.SC72的数据插入 1.构建数据库 2.构建表 3.插入数据 插入数据语句: ① 命令: INSERT INTO TableNameVALUES('值', ...

  4. 第一行代码中RecyclerView添加依赖库问题

    现在更新到 implementation 'com.android.support:recyclerview-v7:29.2.1' 记得点Sync Now来进行同步.

  5. css3 @keyframe 抖动/变色动画

    一.纯css实现 .shake{    //抖动的元素    width: 200px;    height: 100px;    margin: 50px auto;    background: ...

  6. CI框架导入 excel

    整合PHP  Excel和PHPexcelReader到 librarys下面     两个excel整合成一个excel   <?php class Excel extends Control ...

  7. [阿里DIN] 从模型源码梳理TensorFlow的乘法相关概念

    [阿里DIN] 从模型源码梳理TensorFlow的乘法相关概念 目录 [阿里DIN] 从模型源码梳理TensorFlow的乘法相关概念 0x00 摘要 0x01 矩阵乘积 1.1 matmul pr ...

  8. Servlet基础使用总结

    Servlet通俗理解:主要功能在于交互式地浏览和生成数据,生成动态Web内容.Servlet运行于支持Java的应用服务器中.从原理上讲,Servlet可以响应任何类型的请求,但绝大多数情况下Ser ...

  9. mysql yyyy-MM-dd function UNIX_TIMESTAMP('yyyy-MM-dd HH:mm:ss')

    mysql yyyy-MM-dd function UNIX_TIMESTAMP('yyyy-MM-dd HH:mm:ss') select UNIX_TIMESTAMP('1997-10-04 22 ...

  10. Mac环境MySql初始密码设置

    1. 首先 点击系统偏好设置 -> 点击MySQL, 在弹出的页面中,关闭服务.2. 进入终端命令输出: cd /usr/local/mysql/bin/ 命令,回车.3. 回车后,输入命令:s ...