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].

 
思路:
找规律递归 一个数跟自己后面的数字交换如 1 2 2 第一个数字和第2个数字换, 但是不换重复的数字 第一个数字和第三个数字就不换了
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)的更多相关文章

  1. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  2. 【leetcode】Permutations II

    Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...

  3. 【leetcode】Subsets II (middle) ☆

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  4. 【LeetCode】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

  5. 【LeetCode】课程表 II

    [问题]现在你总共有 n 门课需要选,记为 0 到 n-1.在选修某些课程之前需要一些先修课程.例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示他们: [0,1]给定课程总量以及 ...

  6. 【leetcode】Permutations (middle)

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  7. 【leetcode】Permutations

    题目描述: Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the ...

  8. 【leetcode】N-Queens II

    N-Queens II Follow up for N-Queens problem. Now, instead outputting board configurations, return the ...

  9. 【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 ...

随机推荐

  1. DOS批处理命令-call命令

    call命令 在批处理中调用别的批处理或者可运行程序或者 バッチ プログラムを別のバッチ プログラムから呼び出します. 语法 1.CALL [驱动盘符:][路径]文件名 [参数] 调用并执行[驱动盘符 ...

  2. 实现网页页面跳转的几种方法(meta标签、js实现、php实现)

    1.meta标签实现 只需在head里加上下面这一句就行了,在当前页面停留0.1秒后跳转到目标页面  代码如下 复制代码 1 <meta http-equiv="refresh&quo ...

  3. Android的selector,背景选择器

    原文地址 http://android.blog.51cto.com/268543/564581 首先android的selector是在drawable/xxx.xml中配置的,相关图片放在同目录下 ...

  4. jquery-ui 中treegird 逐步加载

    官方网站上没有ajax逐步加载的例子,自己研究了下 js代码 $("#bomStructureTable").treegrid({ url : "systemcontro ...

  5. Java编程思想之字符串

    来自:Java编程思想(第四版) 第十三章 字符串   字符串操作是计算机程序中最常见的行为.   String对象是不可变的.查看JDK文档你就会发现,String类中每一个看起来会修改String ...

  6. Android - 代码片段

    转载说明 本篇文章可能已经更新,最新文章请转:http://www.sollyu.com/android-code-snippets/ 说明 此篇文章为个人日常使用所整理的一此代码片段,此篇文正将会不 ...

  7. Poj OpenJudge 百练 1573 Robot Motion

    1.Link: http://poj.org/problem?id=1573 http://bailian.openjudge.cn/practice/1573/ 2.Content: Robot M ...

  8. 隐藏内容_网络推广_seo中级视频教程详解

    课程背景:SEO(Search Engine Optimization),汉译为搜索引擎优化.搜索引擎优化是一种利用搜索引擎的搜索规则来提高目的网站在有关搜索引擎内的排名的方式.SEO目的理解是:为网 ...

  9. Kail安装Parallels tools

    今天在安装虚拟机里面安装kail,在安装虚拟机提供的tools时候提示没有权限,如图: 后面经过自己证实首先tools是挂载的cdrom,在这样挂载下是没有写权限的(类似于windows下面读取光盘光 ...

  10. javascript 代码优化工具 UglifyJS

    安装: 1. 安装 node.js 环境 (这个不用我教了吧,网上教程一大堆哦.) 2. 进入 https://github.com/mishoo/UglifyJS  右上角 “Download” Z ...