求全排列。

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. kafka生产者和消费者

    在使用kafka时,有时候为验证应用程序,需要手动读取消息或者手动生成消息.这个时候可以借助kafka-console-consumer.sh和kafka-console-producer.sh 这两 ...

  2. 架构和性能优化的核心原则(康神sf讲座学习笔记)

    其实架构性能优化的核心就是分,分为分离.分层.分布. 分离动静分离静态资源.动态页面的分离 比如,一个页面有很多静态图片,静态的图片.动态数据.静态CSS.js,图片一般用cdn,但静态资源在使用域名 ...

  3. 编译时错误之 error C2338: tuple_element index out of bounds

    part 1 编译器 vs2015 VC++. 完整的错误信息粘贴如下: d:\program files (x86)\microsoft visual studio 14.0\vc\include\ ...

  4. 最大子段和SP1716GSS3 线段树

    前言 spoj需要FQ注册,比较麻烦,大家就在luogu评测吧 题目大意: $n$ 个数,$q$ 次操作 操作$0 _ x_ y$把$A_x$ 修改为$y$ 操作$1 _ l _r$询问区间$[l, ...

  5. centos6搭建redis集群搭建(单机多节点)

    一.安装redis 1.安装gcc环境 yum install gcc-c++ 2.下载源码包并解压 wget http://download.redis.io/releases/redis-3.2. ...

  6. javaweb 实战_1

    购物车项目 Primary SQL语句 product create table product ( id ) default null, name varchar() default null, p ...

  7. gulp常用命令

    gulp 默认的执行的命名文件为gulpfile 换成其他命名就识别不了 因为需要安装两次gulp或者说其他插件,一个是全局-g安装一个是本地目录安装, 本地目录安装时目录移动或者名字被改变就会失效提 ...

  8. MapReduce程序(一)——wordCount

    写在前面:WordCount的功能是统计输入文件中每个单词出现的次数.基本解决思路就是将文本内容切分成单词,将其中相同的单词聚集在一起,统计其数量作为该单词的出现次数输出. 1.MapReduce之w ...

  9. C# 新Form各事件执行顺序

    1. 构造函数 2. Load() 3. Show() 4. Acticated()

  10. DataSet 动态添加列

    public DataSet GetNewId(List<string> IdArr) { DataSet ds = new DataSet(); DataTable newtb = ne ...