求全排列。

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.

vector<vector<int>> permute(vector<int>& nums) {
int n = nums.size(), l, i, j, k;
vector<vector<int>> ans;
ans.push_back(nums);
for(k = ; k < n-; k++)
{
l = ans.size();
for(i = ; i < l; i++)
{
for(j = ; k+j < n; j++)
{
vector<int> v = ans[i];
swap(v[k], v[k+j]);
ans.push_back(v);
}
}
}
return ans;
}

2. 有重复元素

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

vector<vector<int>> permuteUnique(vector<int>& nums) {
int n = nums.size(), l, i, j, k;
vector<vector<int>> ans;
ans.push_back(nums);
for(k = ; k < n-; k++)
{
l = ans.size();
for(i = ; i < l; i++)
{
sort(ans[i].begin()+k, ans[i].end());
for(j = ; k+j < n; j++)
{
vector<int> v = ans[i];
if(v[k+j] == v[k+j-])
continue;
swap(v[k], v[k+j]);
ans.push_back(v);
}
}
}
return ans;
}

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. 如何使用Unity制作虚拟导览(一)

    https://www.cnblogs.com/yangyisen/p/5108289.html Unity用来制作游戏已经是目前市场上的一个发展趋势,而且有越来越多的公司与开发者不断的加入,那么Un ...

  2. MySQL之表连接(内外连接和重命名的使用)

    #要多练练 1.连接查询根据连接方式分为 内连接 等值连接 非等值连接 自连接 外连接 左外连接(左连接) 右外连接(右连接) 当多张表进行连接查询,若没有任何条件进行限制,会 发生什么现象? 会出现 ...

  3. P3008 [USACO11JAN]道路和飞机Roads and Planes

    P3008 [USACO11JAN]道路和飞机Roads and Planes Dijkstra+Tarjan 因为题目有特殊限制所以不用担心负权的问题 但是朴素的Dijkstra就算用堆优化,也显然 ...

  4. Python3 Selenium自动化-select下拉框

    Python3 Selenium自动化-select下拉框 selenium介绍select下拉框相关的操作方法:

  5. Python3.x(windows系统)安装requests库

    Python3.x(windows系统)安装requests库 cmd命令: pip install requests 执行结果:

  6. JavaScript Match

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

  7. PHP 开发 api 接口安全验证

    原文链接:http://blog.csdn.net/li741350149/article/details/62887524

  8. 关于STM32外接4—16MHz晶振主频处理方法

    由于STM32F10x库官方采用的是默认的外接8MHz晶振,因此造成很多用户也采用了8MHz的晶振,但是,8MHz的晶振不是必须的,其他频点的晶振也是可行的,只需要在库中做相应的修改就行.    在论 ...

  9. Pycharm 2017 激活码

    Pycharm 2017 激活码 server选项里边输入:  http://idea.liyang.io 我是通过这个成功的

  10. PHP Extension

    新手搞PHP ,之前用过 PERL, BASH: 所以开始用PHP 写程序上手比较快, 几天之后对PHP 的内部实现机制产生了兴趣,所以自己尝试着写写简单的PHP 扩展,以增加对PHP 的理解.   ...