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 ...
随机推荐
- Postgresql 珍藏级文章
https://wiki.postgresql.org/wiki/Tuning_Your_PostgreSQL_Server 如何设置参数值 https://www.cnblogs.com/zhao ...
- FileMaker Server连接SQL Server测试
用FM测试了一把扫二维码.效果还不错,简单的设置几下就可以上线,使用Iphone扫二维码进行盘点以及更新照片功能.接下来测试下下ODBC连接. FMS连接SQL Server测试 1. 在FMS服务器 ...
- U3D学习002——编辑器使用
shift键+鼠标点击中间方块,实现视角切换回初始化透视模式 Unity GameObject菜单栏下有3个和View(此View指显示Scene面板虚拟相机(后面简称Scene Camera)的视角 ...
- mybatis的插件,挺好支持下
利用 Mybatis-generator自动生成代码http://www.cnblogs.com/yjmyzz/p/4210554.html Mybatis 通用 Mapper3 https://gi ...
- JPA查询
Pojo: UserDetails EntityManager: entityManager 1. Ceate Criteria CriteriaBuilder builder = entityMan ...
- Visual Studio 2012 & MyEclipse2015 快捷键对比
- for循环循环时间
)) { Console.WriteLine(dt); } ("2011-5-5") 按需求定义 AddDays函数, 一天一天的增长
- es6基础(2)--解构赋值
//解构赋值 { let a,b,rest; [a,b]=[1,2]; console.log(a,b); } //数组解构赋值 { let a,b,rest; [a,b,...rest]=[1,2, ...
- NEU(Fst Network Embedding Enhancement via High Order Proximity Approximation)
NEU(Fst Network Embedding Enhancement via High Order Proximity Approximation) NEU:通过对高阶相似性的近似,加持快速网络 ...
- 网络表示学习Network Representation Learning/Embedding
网络表示学习相关资料 网络表示学习(network representation learning,NRL),也被称为图嵌入方法(graph embedding method,GEM)是这两年兴起的工 ...