LeetCode OJ 47. Permutations II
题目
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,
[1,1,2] have the following unique permutations:
[
[1,1,2],
[1,2,1],
[2,1,1]
]
解答
C语言的实现以前总结过,请见 http://www.cnblogs.com/YuNanlong/p/6171459.html
这道题做的我很气,自己写交换vector元素的过程,只击败了30+%的提交,用了swap()函数击败了70+%的。。。
下面是AC的代码:
class Solution {
public:
vector<vector<int>> ans;
vector<int> res;
void permute(vector<int> nums){
int length = nums.size();
if(length == 1){
res.push_back(nums[0]);
ans.push_back(vector<int>(res.begin(), res.end()));
res.pop_back();
return ;
}
else{
int last;
for(int i = 0; i < length; i++){
if(i == 0){
last = nums[i];
}
else if(last == nums[i]){
continue;
}
last = nums[i];
swap(nums[0], nums[i]);
res.push_back(last);
permute(vector<int>(nums.begin() + 1, nums.end()));
res.pop_back();
}
}
}
vector<vector<int>> permuteUnique(vector<int>& nums) {
sort(nums.begin(), nums.end());
permute(vector<int>(nums.begin(), nums.end()));
return ans;
}
};
107
LeetCode OJ 47. Permutations II的更多相关文章
- [Leetcode][Python]47: Permutations II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...
- 【一天一道LeetCode】#47. Permutations II
一天一道LeetCode系列 (一)题目 Given a collection of numbers that might contain duplicates, return all possibl ...
- 【LeetCode】47. Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- 【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 ...
- LeetCode OJ:Permutations II(排列II)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] 47. Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- leetCode 47.Permutations II (排列组合II) 解题思路和方法
Permutations II Given a collection of numbers that might contain duplicates, return all possible un ...
- leetcode46. Permutations 、47. Permutations II、 剑指offer字符串的排列
字符串排列和PermutationsII差不多 Permutations第一种解法: 这种方法从0开始遍历,通过visited来存储是否被访问到,level代表每次已经存储了多少个数字 class S ...
随机推荐
- Css学习(4)
文档流(标准流) 元素自上而下,自左而右,块元素独占一行,行内元素在一行上显示,碰到父集元素的边框换行. 浮动布局 float: left | right 特点: ★元素浮动之后不占据原来的 ...
- tf.nn.embedding_lookup
tf.nn.embedding_lookup(params, ids, partition_strategy=’mod’, name=None, validate_indices=True, max_ ...
- CRM 2016 执行IFrame 子页面中函数
CRM代码: var iframe = Xrm.Page.getControl("IFRAME_xxx").getObject(); iframe.contentWindow.Re ...
- C++删除容器数据
// free the contents of the list; erase the list inline void ListDelete (list <void *> *pList) ...
- cas server
Tomcat: V8.5.x Java: 1.8 x64 MySQL: 5.5.x OS: Win10 x64 I. war 0. clone git clone https://github.com ...
- MariaDB MaxScale
1. down https://mariadb.com/downloads/#mariadb_platform-mariadb_maxscale (1) install sudo yum locali ...
- 初级安全入门—— WEBshell与文件上传漏洞
概念介绍 WebShell网页木马文件 最常见利用文件上传漏洞的方法就是上传网站木马(WebShell)文件,根据开发语言的不同又分为ASP木马.PHP木马.JSP木马等,该木马利用了脚本语言中的系统 ...
- RocketMQ源码分析:(一)安装与案例演示
环境: Windows 暂时理解的功能图,不定时改动: 1. 克隆rocketmq代码 git clone git@github.com:apache/rocketmq.git 2. 进入rocket ...
- 关于百度world 编辑器改变上传图片的保存路径图片不显示的问题
在ueditor.mini for asp.net 中,将上传的图片保存的路径更改了,可图片在 world 编辑器中不显示,但却可以上传到指定的保存目录下,解决这个问题的方法 是: 在udditor_ ...
- mke2fs
mke2fs - create an ext2/ext3/ext4 filesystem # mke2fs -b -c -N -t ext4 /dev/sdb #(注意末尾是/dev/sdb....? ...