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 ...
随机推荐
- HTTP协议的简单解析
超文本传输协议(HTTP,HyperText Transfer Protocol)是用于从服务器传输超文本到本地浏览器的传输协议,是应用最为广泛的网络协议.B/S网络架构的核心是HTTP,掌握HTTP ...
- 一个windows计划任务的Nginx日志自动截断的批处理命令
net stop nginx taskkill /im nginx.exe /f cd E:\nginx e: set NO=%Date:~0,4%%Date:~5,2%%Date:~8,2% set ...
- word_宏示例
参考:https://jingyan.baidu.com/article/870c6fc3326588b03fe4beeb.html 内容自适应 Application.Browser.Target ...
- ScrollView嵌套RecyclerView、ScrollView嵌套Listview、ScrollView嵌套各种布局,默认不在顶部和回到顶部的解决方法;
如果: ScrollView.scrollTo(0,0): ScrollView.fullScroll(View.FOCUS_UP) : ScrollView.smoothScrollTo(0, 0) ...
- CF603EPastoral Oddities
/* LCT管子题(说的就是你 水管局长) 首先能得到一个结论, 那就是当且仅当所有联通块都是偶数时存在构造方案 LCT动态加边, 维护最小生成联通块, 用set维护可以删除的边, 假如现在删除后不影 ...
- vue-cli 项目搭建
vue-cli 项目搭建 1.首先需要安装nodejs(安装省略). 2.用node安装vue-cli. npm install -g vue-cli 3.新建目录用来存放工程. 新建一个vue项 ...
- SpringBoot入门 (十) 发送邮件
本文记录学习在SpringBoot中发送邮件. 一 邮件发送过程 发送邮件是一个我们在项目中经常会用到的功能,如在用户注册时发送验证码,账户激活等都会用到.完整的一个邮件发送过程主要包含以下几个步骤: ...
- idea 和eclipse的debug调试快捷键对比
IntelliJ Idea 常用快捷键列表 Ctrl+Shift + Enter,语句完成“!”,否定完成,输入表达式时按 “!”键Ctrl+E,最近的文件Ctrl+Shift+E,最近更改的文件Sh ...
- ES6数组的新增功能,还是很强大的好多地方用的到
map,reduce,filter ,forEach 废话不多说代码走起 map 映射 就是一个对一个 /* 比如:学生成绩分数对应及格不及格 也可以做一些算数运算 */ let chengji ...
- 阿里Canal配置(编写中)
首先在源mysql的.ini文件中进行配置 [client]default-character-set=utf8 [mysqld]basedir = E:/testCanal/mysql-5.7.17 ...