题目要求: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. LORA串口无线数据透明传输终端ZSL311

    ZSL311是由成都众山科技生产销售的一款LORA串口无线数据透明传输终端,采用的是LoRa扩频技术来进行无线数据传输,同时提供RS485和RS232串口,为用户提供全透明数据传输模式.支持星形.Me ...

  2. wepack配置

    一.什么是 webpack? webpack是一款模块加载器兼打包工具,它能把各种资源,例如JS(含JSX).coffee.样式(含less/sass).图片等都作为模块来使用和处理,它能有Grunt ...

  3. Python使用JsAPI发起微信支付 Demo

    Python使用JsAPI发起微信支付 Demo 这个是基于Django框架. 了解更多,可以关注公众号"轻松学编程" 1.公众号设置.微信商户号设置 这些都可以在官网查得到, 公 ...

  4. 使用 scrapy-redis实现分布式爬虫

    Scrapy 和 scrapy-redis的区别 Scrapy 是一个通用的爬虫框架,但是不支持分布式,Scrapy-redis是为了更方便地实现Scrapy分布式爬取,而提供了一些以redis为基础 ...

  5. 【CHOJ】磁力块

    题意描述 磁力块 在平面内分布着 \(N\) 个磁力块,同时你的手上也有一块. 你一开始站在给定的坐标上,当磁力块之间满足互相吸引的条件时就可以吸引. 当你拿到新的磁石时你就可以用它来吸引更多的石头, ...

  6. Spring Cloud Alibaba 之 版本选择

    alibaba 版本问题 一下是Spring cloud ,Spring Cloud Alibaba, Spring Boot 之间的版本选择 在版本选择上大家尽量选择稳定版,也就是Release 后 ...

  7. 多服务器使用Docker设置一主一从三哨兵redis(完整)

    本来应该续之前那篇博客Docker配置redis哨兵模式--多服务器·上写一个下篇的,但是忽然意识到应该将必要的环境打包为一个基础镜像,在此基础上建立与redis有关的镜像,这样既能够快速打包,又能够 ...

  8. 7、Python语法之与用户交互、运算符

    一 .程序与用户交互 1.1.什么是与用户交互 用户交互就是人往计算机中input/输入数据,计算机print/输出结果. 1.2.为什么要与用户交互 为了让计算机能够像人一样与用户沟通交流. 比如, ...

  9. 《我想进大厂》之Java基础夺命连环16问

    说好了面试系列已经完结了,结果发现还是真香,嗯,以为我发现我的Java基础都没写,所以这个就算作续集了,续集第一篇请各位收好. 说说进程和线程的区别? 进程是程序的一次执行,是系统进行资源分配和调度的 ...

  10. C语言I博客作业3

    这个作业属于哪个课程 <https://edu.cnblogs.com/campus/zswxy/SE2020-1 > 这个作业要求在哪里 https://edu.cnblogs.com/ ...