【leetcode】Permutations II (middle)
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], and [2,1,1].
class Solution {
public:
vector<vector<int> > permuteUnique(vector<int> &num) {
vector<vector<int>> ans;
if(num.empty())
return ans;
permute(ans, num, );
return ans;
}
//不知道为什么 注释掉的这种 在我自己的电脑上就ok 但是提交就总是Output Limit Exceeded??
//void permute(vector<vector<int>> &ans, vector<int> num, int k)
//{
// if(k >= num.size())
// {
// ans.push_back(num);
// return;
// }
// for(int j = k; j < num.size(); j++) //和自己或后面交换
// {
// if (j > k && num[j] == num[j-1]) continue; //prevent duplicates
// swap(num[k], num[j]);
// permute(ans, num, k + 1);
// swap(num[k], num[j]);
// }
//}
void permute(vector<vector<int>> &ans, vector<int> num, int k)
{
if(k >= num.size())
{
ans.push_back(num);
return;
}
vector<int> hash;
for(int j = k; j < num.size(); j++) //和自己或后面交换
{
if(find(hash.begin(), hash.end(), num[j]) == hash.end())
{
hash.push_back(num[j]);
swap(num[k], num[j]);
permute(ans, num, k + );
swap(num[k], num[j]);
}
}
}
};
【leetcode】Permutations II (middle)的更多相关文章
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【leetcode】Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- 【leetcode】Subsets II (middle) ☆
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- 【LeetCode】课程表 II
[问题]现在你总共有 n 门课需要选,记为 0 到 n-1.在选修某些课程之前需要一些先修课程.例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示他们: [0,1]给定课程总量以及 ...
- 【leetcode】Permutations (middle)
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- 【leetcode】Permutations
题目描述: Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the ...
- 【leetcode】N-Queens II
N-Queens II Follow up for N-Queens problem. Now, instead outputting board configurations, return the ...
- 【leetcode】 Permutation Sequence (middle)
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
随机推荐
- php对UTF8字体串进行单字分割返回数组
在网上查了很多字符串分割方法,都无法正确对UTF8字符串进行正确分割返回单个字符的数组.经过对FTU8编码的分析写出了下面的方法对UTF8进行分割.本人测试可用.本方法只支持UTF8编码的,其它编码转 ...
- 找不到System.Runtime.Serialization.Json的解决方案
System.ServiceModel System.ServiceModel.Web System.Runtime.Serialization 三者均要添加引用
- javascript笔记——js的阻塞特性[转载]
JS具有阻塞特性,当浏览器在执行js代码时,不能同时做其它事情,即<script>每次出现都会让页面等待脚本的解析和执行(不论JS是内嵌的还是外链的),JS代码执行完成后,才继续渲染页面. ...
- 为mapcontrol中的图层设置透明度
ILayer pLayer = axMapControl1.get_Layer(); ILayerEffects pLayerEffects = (ILayerEffects)pLayer; pLay ...
- ArcSDE for oracle10g安装后post的时候出现错误
The Post Installation Setup can not locate required Oracle files in your path.Check your Oracle inst ...
- 实现android上解析Json格式数据功能
实现android上解析Json格式数据功能,该源码转载于安卓教程网的,http://android.662p.com ,个人感觉还不错的,大家可以看看一下吧. package com.practic ...
- qml自定义标题栏
要实现自定义的标题栏只需在原来的窗口的基础上创建一个Rectangle并将其定位在窗口顶部即可,实现代码如下: ApplicationWindow { id: mainWindow visible: ...
- c#汉字转为拼音
using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressi ...
- Xcode更改配色方案
更改配色方案:Xcode > PReferences > Fonts & Color /********************************************** ...
- php+mysql非缓冲查询(如何循环大数组)
另外一种PHP查询模式是非缓冲查询,数据库服务器会一条一条的返回数据,而不是一次全部返回,这样的结果就是PHP程序消耗较少的内存,但却增加了数据库服务器的压力,因为数据库会一直等待PHP来取数据,一直 ...