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. UMeng 友盟的用户数,启动数 等

    最近一哥们研究UMeng 友盟的用户数,启动数,设备和位置相关数据,发现真的可以模拟啊. 支持Android,IOS,WPhone平台,同时可以实现每小时按10万级的速度增加,启动次数也是可以按10万 ...

  2. Adapter模式

    Adapter模式主要用于将一个类的接口转换为另外一个接口,通常情况下再不改变原有体系的条件下应对新的需求变化,通过引入新的适配器类来完成对既存体系的扩展和改造.实现方式主要包括: 1.类的Adapt ...

  3. Alluxio1.0.1最新版(Tachyon为其前身)介绍,+HDFS分布式环境搭建

    Alluxio(之前名为Tachyon)是世界上第一个以内存为中心的虚拟的分布式存储系统.它统一了数据访问的方式,为上层计算框架和底层存储系统构建了桥梁. 应用只需要连接Alluxio即可访问存储在底 ...

  4. OCI的结果输出

    绑定变量,把结果以列的方式输出到每一字段输出到一个数组里

  5. 基于css3新属性transform及原生js实现鼠标拖动3d立方体旋转

    基于css3新属性transform,实现3d立方体的旋转 通过原生JS,点击事件,鼠标按下.鼠标抬起和鼠标移动事件,实现3d立方体的拖动旋转,并将旋转角度实时的反应至界面上显示 实现原理:通过获取鼠 ...

  6. PHP中使用Luhn算法校验信用卡及借记卡卡号

    Luhn算法会通过校验码对一串数字进行验证,校验码通常会被加到这串数字的末尾处,从而得到一个完整的身份识别码. 我们以数字“7992739871”为例,计算其校验位: 从校验位开始,从右往左,偶数位乘 ...

  7. 非关系型数据库SequoiaDB虚拟机下应用初探

    SequoiaDB是广州巨杉软件有限公司开发的一款新型分布式非关系型数据库.可应用于linux操作系统下.在虚拟机下试用了一下(操作系统Ubuntu),感觉不错,操控简单易上手,在此分享一下心得. 下 ...

  8. 51nod1417 天堂里的游戏

    ---恢复内容开始--- 1417 天堂里的游戏 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  关注 多年后,每当Noder看到吉普赛人,就会想起那个遥 ...

  9. PHP7 新特性 简介

    整理了一些常用的新特性,欢迎点赞!!! 新增操作符 1.?? $username = $_GET['user'] ?? ''; $username = isset($_GET['user']) ? $ ...

  10. 《ENVI下遥感影像自然真彩色合成方法》——TM、spot5

    来源:http://blog.sina.com.cn/s/blog_764b1e9d0100tz4f.html#bsh-73-375271630