46. 47. Permutations
求全排列。
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的更多相关文章
- 46. 47. Permutations and Permutations II 都适用(Java,字典序 + 非字典序排列)
解析: 一:非字典序(回溯法) 1)将第一个元素依次与所有元素进行交换: 2)交换后,可看作两部分:第一个元素及其后面的元素: 3)后面的元素又可以看作一个待排列的数组,递归,当剩余的部分只剩一个元素 ...
- [Leetcode][Python]47: Permutations II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...
- 31. Next Permutation + 46. Permutations + 47. Permutations II + 60. Permutation Sequence
▶ 问题:字典序生成有关的问题. ▶ 31. 由当前序列生成字典序里的下一个序列. ● 初版代码,19 ms class Solution { public: void nextPermutation ...
- 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 ...
- 【一天一道LeetCode】#47. Permutations II
一天一道LeetCode系列 (一)题目 Given a collection of numbers that might contain duplicates, return all possibl ...
- [LeetCode] 47. Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] 47. Permutations II 全排列 II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【LeetCode】47. Permutations II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
- [leetcode] 47. Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
随机推荐
- c++第十四天
<c++ primer, 5E> 第91页到第94页,笔记: 1.vector支持的操作. v.empty().v.size().v.push_back(t).v[n] 2.试图通过下标访 ...
- SNMP学习笔记之Python的netsnmp和pysnmp的性能对比
0x00 概览 用python获取snmp信息有多个现成的库可以使用,其中比较常用的是netsnmp和pysnmp两个库.网上有较多的关于两个库的例子. 本文重点在于如何并发的获取snmp的数据,即同 ...
- kubernetes extension point
以下大部分来自于k8s document, 笔者只是总结归纳, 解释不足的地方请参阅相关文档 Intention Non-sustainable way to customize Kubernetes ...
- 使用 Vue.js 结合bootstrap 实现的分页控件
原文链接:http://blog.csdn.net/qiuhaotc/article/details/53031884 源码下载: http://pan.baidu.com/s/1i4XgH6H 密码 ...
- WIFI模块对比介绍
一.ESP8266(官网 https://espressif.com/)1 简介 乐鑫智能互联平台——ESCP 拥有高性能无线SOC,给移动平台设计师带来福音,它 以最低成本提供最大实用性,为WiFi ...
- Jackson 使用和注意项
依赖maven: <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId ...
- 2、extract-text-webpack-plugin提取Sass编译的Css
cnpm install css-loader --save-dev //css-loader 是将css打包进js cnpm install style-loader --save-dev ...
- onchange()事件的应用
本文为博主原创,未经允许不得转载: jQuery提供了很多很强大的事件,想要都掌握发现难度蛮大的,只有在不断的应用与实践中学习和掌握. 在做页面的时候,想做一个在选择下拉框选择值的时候,系统根据下拉框 ...
- Node.js中package.json中^和~的区别
webpack 项目的package.json 文件列出了项目所依赖的插件和库,同时也给出了对应的版本说明,但是在版本说明前面还有个符号:'^'(插入符号)和'~'(波浪符号),总结了下他们之间的区别 ...
- TinyXML用法小结2
参考:http://www.cnblogs.com/hgwang/p/5833638.html TinyXML用法小结 1. 介绍 Tinyxml的官方网址:http://www.grinn ...