题目要求: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. Vue+Antd搭配百度地图实现搜索定位等功能

    前言 最近,在做vue项目的时候有做到选择地址功能,而原项目中又引入了百度地图,所以我就打算通过使用百度地图来实现地址搜索功能啦. 本次教程可能过于啰嗦,所以这里先放上预览地址供大家预览--点我预览, ...

  2. 机器学习之K均值算法(K-means)聚类

    K均值算法(K-means)聚类 [关键词]K个种子,均值 一.K-means算法原理 聚类的概念:一种无监督的学习,事先不知道类别,自动将相似的对象归到同一个簇中. K-Means算法是一种聚类分析 ...

  3. Docker(7)- docker images 命令详解

    如果你还想从头学起 Docker,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1870863.html 作用 列出所有的本地镜像 语法格 ...

  4. npm--npm+gulp发布至私服,报错E503解决方案

    由于项目共享组件库的需要,我们搭建了npm私有服务器,供本公司几个项目可以访问.组件库使用gulp+webpack+npm进行打包构建,私服使用的是 Verdaccio直接搭建的,一键式傻瓜搭建,贼好 ...

  5. js &&与||的妙用

    &&表达式中,若前一个为false则不会执行下去,||表达式中,一直寻找到true即停止 例:成长速度为5显示1个箭头,为10显示2个箭头,为 15显示3个箭头,其余显示0个箭头var ...

  6. 判断浏览器,还在用userAgent吗,你out了

    以下内容摘自http://www.cnblogs.com/rubylouvre/archive/2009/10/14/1583362.html //2010 4 16日更新 ie678 = !+&qu ...

  7. CSS不同颜色按钮实例

    CSS减少代码重复技巧非常多,以主要包含采用相对尺寸.半透明颜色的实例来说明CSS减少代码重复技巧的一些运用. 以下为通过CSS代码实现的一个"Yes!"按钮效果以及相应的代码: ...

  8. 前端动画框架GSAP框架随笔

    gsap是目前非常流行的前端动画框架,可以非常轻松构造出复杂的动画效果,这里仅对我实际使用中的一些例子进行总结 官网 示例 文章种所使用代码的在线示例 基础用法 // 声明一个滚动控制器 let ct ...

  9. 无字母数字getshell

    无字母数字webshell 预备知识 一些不包含数字和字母的webshell https://www.leavesongs.com/PENETRATION/webshell-without-alpha ...

  10. 在Linux下的安装mysql-5.7.28 心得总结

    mysql-5.7.28 在Linux下的安装教程图解 这篇文章主要介绍了mysql-5.7.28 的Linux安装,本文通过图文并茂的形式给大家介绍的非常详细,具有一定的参考借鉴价值,希望给有需要的 ...