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 ...
随机推荐
- conda命令
- Python工程化小结
对如何写一个工业级的Python项目作一个top-down小结. 一.项目结构 顶层结构: 文件夹: model可以是项目中的自定义类: utils是一些工程工具,比如log,tracker log存 ...
- Meet in the middle学习笔记
Meet in the middle(MITM) Tags:搜索 作业部落 评论地址 PPT中会讲的很详细 当搜索的各项互不影响(如共\(n\)个物品前\(n/2\)个物品选不选和后\(n/2\)个物 ...
- HBase 集群部署
前提条件:hadoop及zookeeper机群已经搭建好. 配置hbase集群步骤: 1.配置hbase集群,要修改3个文件 注意:要把hadoop的hdfs-site.xml和core-site. ...
- 配置Jsp错误页面
配置Jsp错误页面一般我们有2种做法: (1)在页面中用指令进行配置,即page指令的errorPage和isErrorPage:可以使用page指令的errorPage来指定错误页!在当前JSP页面 ...
- mint-ui 输入框按下按键执行查询
环境:vue.mint-ui 功能:一个输入框,按下按键之后就执行某个功能. 截图:一个输入框 输入框html: <mt-search v-model="query" can ...
- android:windowSoftInputMode属性;界面关闭后软键盘不隐藏的解决方法;
stateUnspecified:软键盘的状态并没有指定,系统将选择一个合适的状态或依赖于主题的设置 stateUnchanged:当这个activity出现时,软键盘将一直保持在上一个activit ...
- Basler和Matrox的配置及调试
说明: 本系列博文是我自己研究生课题,采用做一步记录一步,在论文答辩结束或者机器设计结束之后才会附上源代码! 自从装好相机和设计好机械结构之后就没有继续进行下一步,这段时间花了三四天继续上次任务进行, ...
- js 截取指定字符长度 为数组
str要截取的字符 n截取个数 function jiequ(str,n) { var strArr = []; for (var i = 0, l = s ...
- ssh 第一次登录输入yes 问题
#!/bin/bash for i in hadoop01 hadoop02 hadoop03 hadoop04 hadoop05 hadoop06 hadoop07 hadoop08 hadoop0 ...