求全排列。

1. 无重复元素

Given a collection of distinct 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].

* The algroithm - Take each element in array to the first place.
* For example:
* 0) initalization
* pos = 0
* [1, 2, 3]
* 1) take each element into the first place,
* pos = 1
* [1, 2, 3] ==> [2, 1, 3] , [3, 1, 2]
* then we have total 3 answers
* [1, 2, 3], [2, 1, 3] , [3, 1, 2]
* 2) take each element into the "first place" -- pos
* pos = 2
* [1, 2, 3] ==> [1, 3, 2]
* [2, 1, 3] ==> [2, 3, 1]
* [3, 1, 2] ==> [3, 2, 1]
* then we have total 6 answers
* [1, 2, 3], [2, 1, 3] , [3, 1, 2], [1, 3, 2], [2, 3, 1], [3, 2, 1]
* 3) pos = 3 which greater than length of array, return.

  1. vector<vector<int>> permute(vector<int>& nums) {
  2. int n = nums.size(), l, i, j, k;
  3. vector<vector<int>> ans;
  4. ans.push_back(nums);
  5. for(k = ; k < n-; k++)
  6. {
  7. l = ans.size();
  8. for(i = ; i < l; i++)
  9. {
  10. for(j = ; k+j < n; j++)
  11. {
  12. vector<int> v = ans[i];
  13. swap(v[k], v[k+j]);
  14. ans.push_back(v);
  15. }
  16. }
  17. }
  18. return ans;
  19. }

2. 有重复元素

// To deal with the duplication number, we need do those modifications:
// 1) sort the array [pos..n].
// 2) skip the same number.

  1. vector<vector<int>> permuteUnique(vector<int>& nums) {
  2. int n = nums.size(), l, i, j, k;
  3. vector<vector<int>> ans;
  4. ans.push_back(nums);
  5. for(k = ; k < n-; k++)
  6. {
  7. l = ans.size();
  8. for(i = ; i < l; i++)
  9. {
  10. sort(ans[i].begin()+k, ans[i].end());
  11. for(j = ; k+j < n; j++)
  12. {
  13. vector<int> v = ans[i];
  14. if(v[k+j] == v[k+j-])
  15. continue;
  16. swap(v[k], v[k+j]);
  17. ans.push_back(v);
  18. }
  19. }
  20. }
  21. return ans;
  22. }

46. 47. Permutations的更多相关文章

  1. 46. 47. Permutations and Permutations II 都适用(Java,字典序 + 非字典序排列)

    解析: 一:非字典序(回溯法) 1)将第一个元素依次与所有元素进行交换: 2)交换后,可看作两部分:第一个元素及其后面的元素: 3)后面的元素又可以看作一个待排列的数组,递归,当剩余的部分只剩一个元素 ...

  2. [Leetcode][Python]47: Permutations II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...

  3. 31. Next Permutation + 46. Permutations + 47. Permutations II + 60. Permutation Sequence

    ▶ 问题:字典序生成有关的问题. ▶ 31. 由当前序列生成字典序里的下一个序列. ● 初版代码,19 ms class Solution { public: void nextPermutation ...

  4. LeetCode39/40/22/77/17/401/78/51/46/47/79 11道回溯题(Backtracking)

    LeetCode 39 class Solution { public: void dfs(int dep, int maxDep, vector<int>& cand, int ...

  5. 【一天一道LeetCode】#47. Permutations II

    一天一道LeetCode系列 (一)题目 Given a collection of numbers that might contain duplicates, return all possibl ...

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

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

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

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

  8. 【LeetCode】47. Permutations II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

  9. [leetcode] 47. Permutations II

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

随机推荐

  1. python模块-random随机数模块

    导入随机数模块import random 1.random.random() 生成[0,1)之间的随机小数 2.random.randint(a,b) 生成[a,b]之间的随机整数 3.random. ...

  2. maven parent工程.pom修改后未自动更新

    前两周,因为框架parent工程的pom文件做了一点变更,然后在测试服务器进行maven install的时候,死都找不到新的依赖,都把nexus翻了个遍,确定是最新的了,就是download不下来, ...

  3. ORM 关系对象映射 基础知识点

    优点: 1.ORM使我们通用的数据库变得更加的简单便捷. 2.可避免新手程序员写sql语句带来的性能问题. 1. 创建单表 2. 创建关键表 1). 一对一 2). 一对多 3). 多对多 创建表的语 ...

  4. JavaScript Match

    JavaScript Match 版权声明:未经授权,严禁转载! 随机数 // 随机数 Math.random() 随机生成一个大于等于0且小于1的小数. // 0>= r < 1 [0, ...

  5. elasticsearch分词器ik

    1. 下载和es配套的版本 git clone https://github.com/medcl/elasticsearch-analysis-ik 2. 编译 cd elasticsearch-an ...

  6. 10:Python2与Python3比较

    1.print 函数 1. print语句没有了,取而代之的是print()函数. Python 2.6与Python 2.7部分地支持这种形式的print语法. 2.Unicode 1.  在pyt ...

  7. 51NOD 1027 大数乘法

    1027 大数乘法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题   给出2个大整数A,B,计算A*B的结果.   Input 第1行:大数A 第2行:大数B (A,B ...

  8. [oracle复习] - Oracle

    https://deadzq.github.io/oracle/Oracle.html 我的oracle笔记1 https://deadzq.github.io/oracle/Oracle2.html ...

  9. C# WinCE项目 VS2008 单例窗体实现

    项目现有主界面FormMain,模板界面FormModel,其余5个子界面皆继承自模板. 现在想要实现在主界面下可以打开任意子界面,并且可以随时关闭.当打开的子窗体未执行Close事件时,要保证每次显 ...

  10. Mui --- 学习笔记

    1.mui 是选择器,popover 控制显示与隐藏,toggle 自动控制显示或隐藏 function showMenu(){ //mui是选择器 mui('#menu').popover('tog ...