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 ...
随机推荐
- P3313 [SDOI2014]旅行
P3313 [SDOI2014]旅行 树链剖分+动态线段树(并不是lct) 显然的,我们对于每一个宗教都要维护一个线段树. (那么空间不是爆炸了吗) 在这里引入:动态开点线段树 就是需要的点开起来,不 ...
- 轻量级文本标记语言-Markdown
Markdown简介 接触过github的都知道,在发布项目的时候可以建立一个说明文件README.md,这个md文件就是Markdown文本编辑语言的文件. Markdown 是一种轻量级标记语言, ...
- ”MySQL索引“学习总结
序 learn by doing 是最快的学习方式.在百度外卖研发中心,我每天工作接触数据库方面最多的就是"索引",另外面试官在面试时也一定会考察到索引. Part 1, Expl ...
- 05: 配置yum源
1.1 将镜像复制到本地创建yum源 1.将准备好的系统镜像放到指定的目录,本次目录指定在:/dawnfs/sourcecode 2.创建挂载目录:mkdir /mnt/yum 3.挂载镜像: mou ...
- CF#338D. GCD Table
传送门 简单的中国剩余定理练习. 首先行数一定是$lcm$,然后只要确定最小的列数就能判定解合不合法了. 我们可以得到线性模方程组: $y \equiv 0 \pmod{a_1}$ $y+1 \equ ...
- 20145321 《网络对抗技术》 Web安全基础实践
20145321<网络对抗技术> Web安全基础实践 基础问题回答 1.SQL注入攻击原理,如何防御? SQL注入就是通过把SQL命令插入到“Web表单递交”或“输入域名”或“页面请求”的 ...
- 20145334赵文豪网络对抗Web安全基础实践
1.SQL注入攻击原理,如何防御? SQL注入攻击就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意SQL命令的目的. 对于SQL注入攻击的防范,我觉 ...
- Android项目开发二
微博客户端开发 本周学习计划 学习布局控件和UI设计相关知识. 微博验证,学习OAuth相关知识. 看懂微博客户端开发部分代码. 把借鉴代码导入到Android Studio中并运行成功. 实际完成情 ...
- JAVA I/O(一)基本字节和字符IO流
最近再看I/O这一块,故作为总结记录于此.JDK1.4引入NIO后,原来的I/O方法都基于NIO进行了优化,提高了性能.I/O操作类都在java.io下,大概将近80个,大致可以分为4类: 基于字节操 ...
- RNN网络【转】
本文转载自:https://zhuanlan.zhihu.com/p/29212896 简单的Char RNN生成文本 Sherlock I want to create some new thing ...