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].

Solution:

对一个没有重复元素的序列做全排序,可以考虑采用递归的思路。n个元素的全排列为n个元素分别作为第一个元素,并加上分别去除首元素剩下元素的全排列。当序列递归到只有一个元素时,

递归终止,一个可行的全排列产生。

class Solution {
public:vector<vector<int> > ans;vector<int> src;int len;void perm(int i){if(i == len - 1){//a new permutation comesvector<int> tmp;for(int i = 0;i < len;i++)tmp.push_back(src[i]);ans.push_back(tmp);return;}else{for(int k = i;k < len;k++){int tmp = src[i];src[i] = src[k];src[k] = tmp;perm(i + 1);src[k] = src[i];src[i] = tmp;}}}vector<vector<int> > permute(vector<int> &num) {len = num.size();if(len == 0) return ans;src = num;perm(0);return ans;}
};

转载于:https://www.cnblogs.com/changchengxiao/p/3608941.html

[LeetCode] Permutations相关推荐

  1. LeetCode Permutations II(有重复元素的全排列)

    问题:给出一个有重复元素的数组,要求输出全排列 思路:先排序,然后使用next_permutation 具体代码参考: https://github.com/wuli2496/OJ/tree/mast ...

  2. LeetCode Permutations(不重复数的所有排列)

    问题:给出一个没有重复的数组,要求输出所有的排列 思路:先排序,然后使用STL中的next_permution 具体参考: https://github.com/wuli2496/OJ/tree/ma ...

  3. [LeetCode] Permutations II 全排列之二

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

  4. LeetCode Permutations

    原题链接在这里:https://leetcode.com/problems/permutations/ 题目: Given a collection of distinct numbers, retu ...

  5. Leetcode - Permutations I,II

    Leetcode - 046 Permutations 全排列问题是回溯的典型例题: 1.可行解的组成形式是给定数组中的所有数的组合,故而大小上可以作为可行解判定条件 2.每次需要在剩下可被选中的集合 ...

  6. LeetCode:Permutations II

    Problems: Given a collection of numbers that might contain duplicates, return all possible unique pe ...

  7. Leetcode: Permutations II

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

  8. [Leetcode] Permutations 全排列

    Permutations I Given a collection of numbers, return all possible permutations. For example, [1,2,3] ...

  9. [LeetCode] Permutations 解题报告

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

最新文章

  1. 最新版,别的可以不用看了,zabbix 监控 esxi
  2. ubuntu编译并调试OpenJDK8源码实践
  3. js符号输入不可用_js禁止输入特殊字符
  4. [js高手之路]构造函数的基本特性与优缺点
  5. java treemap_Java TreeMap putAll()方法与示例
  6. 【51单片机快速入门指南】7:片上EEPROM
  7. 还在用 Redux,要不要试试 GraphQL 和 Apollo?
  8. JAVA设计模式初探之适配器模式(转)
  9. Talkfree7手机电视,IPTV,VOIP-GPS-3G手机,WAP是未来
  10. ANSYS18.2安装教程
  11. python信息安全书籍_2018年信息安全从业者书单推荐
  12. ubuntu中网易云音乐图标打不开应用的问题
  13. 团队开发如何评估工作量
  14. 威联通建立文件服务器的方法,如何管理你的威联通nas文件?这四款神应用让你效率up!up!...
  15. 40w粉整700w活,一支恰饭作品让B站用户直呼“甲方赚了”
  16. python股票数据分析_用Python浅析股票数据
  17. element 前端布局理解经验及好用的属性
  18. 我们可以用TeamViewer免费版做什么?
  19. 基于C#和Sql Server的网上书店管理系统
  20. SIT-board 远程交互式白板的实现

热门文章

  1. oracle locked timed,Oracle里面的用户smsdb无法登录 LOCKED(TIMED)
  2. mysql实体_mysql实体关系
  3. C++ std::condition_variable wait() wait_for() 区别
  4. 记录 之 cat 和 awk gsub 的使用
  5. 如何复位一个流的failbit和eofbit
  6. mysql inner join
  7. 图解:SQL Server SSIS包和job的部署攻略
  8. jquery常用功能
  9. 提高雅思听力速度必须反复练耳朵别无捷径
  10. STL - bitset