求全排列。

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. Notepad++ 主题配色配置

    以下方法来源网络,仅供参考: 方案1: 1.主题名称:Obsidian 2.字体字号:Courier New 10 (这里字体你可以随意选择,让自己满意即可) 3.设置方法:设置---语言格式设置-- ...

  2. $.post 和 $.get 设置同步和异步请求

    由于$.post() 和 $.get() 默认是 异步请求,如果需要同步请求,则可以进行如下使用:在$.post()前把ajax设置为同步:$.ajaxSettings.async = false;在 ...

  3. nginx/ajax跨子域请求的两种现代方法以及403解决

    因为面向互联网的性质,我们公司的大部分系统都采用多子域的方式进行开发和部署,以达到松耦合和分布式的目的,因此子系统间的交互不可避免.虽然通过后台的rpc框架解决了大部分的交互问题,但有些情况下,前端直 ...

  4. 20145310《网络对抗》逆向及Bof基础

    实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序同时包含另一个代码片段,getShe ...

  5. windows下如何获取系统已存在的盘符 【c++】

    #include <iostream> #include "classAh.h" #include <atlstr.h> using namespace s ...

  6. Python3基础 list str转成list

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  7. Zookeeper 概念

    Zookeeper: Zookeeper是一个高可用的分布式管理与协调框架,基于ZAB算法(原子消息广播协议)的实现.该框架能够很好的保证分布式环境中数据的一致性.也只是基于这样的特性,使得Zooke ...

  8. POJ 1751 Highways(最小生成树&Prim)题解

    思路: 一开始用Kruskal超时了,因为这是一个稠密图,边的数量最惨可能N^2,改用Prim. Prim是这样的,先选一个点(这里选1)作为集合A的起始元素,然后其他点为集合B的元素,我们要做的就是 ...

  9. 【第十四章】 springboot + profile(不同环境读取不同配置)

    具体做法: 不同环境的配置设置一个配置文件,例如:dev环境下的配置配置在application-dev.properties中:prod环境下的配置配置在application-prod.prope ...

  10. json封装数据,然后通过创造的标签调用

    <ul class="list"> <li></li> <li></li> <li></li> ...