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. iOS - 字典(NSDictionary)

    1. 字典类型的常用处理 //---------------不可变字典 //1.字典的创建 NSArray *array1 = [NSArray arrayWithObjects:@"zha ...

  2. Factory Method模式

    Factory Method(工厂方法)模式属于GoF设计模式中的创建型分组,它用于在不之制定待创建对象的具体类的情况下创建对象.--<asp.net设计模式> 说白了就是一个人想要出国旅 ...

  3. c#基础汇总-------------封装

    说到封装,其实是比较基础类的问题,它为程序设计提供了系统与系统,模块与模块,类与类之间交互的实现手段.在.Net中,一切看起来都已经被包装在.Net FrameWork这一复杂的网络中,提供给最终开发 ...

  4. 0<=i<iLen 在C++中

    for( i=0;0<= i<2; i++)这样的话会出现什么错误呢? 一直循环下去, 因为i>=一直成立

  5. doctype的种类

    现实生活中人们使用的浏览器是各式各样的!为了防止各各浏览器页面设置不一样而照成我们所做的网页乱序,不得不设置doctye(文档类型):doctype有3中类型:* 过渡的(Transitional): ...

  6. TestNG使用总结

    1.Eclipse中安装TestNG的方法 打开Eclipse   Help ->Install New Software ,   然后Add   "http://beust.com/ ...

  7. NSS_03 过滤器

    asp.net mvc3有四类过滤器:授权, 操作,结果, 异常.操行的顺序为:授权,操作,结果,异常. 首先看一下TempData: 数据只能经过至多一次的Controller传递, 并且每个元素至 ...

  8. .net 页面跳转方式【转】

    1 Response.Redirect这个跳转页面的方法跳转的速度不快,因为它要走2个来回(2次postback),但他可以跳 转到任何页面,没有站点页面限制(即可以由雅虎跳到新浪),同时不能跳过登录 ...

  9. 一款jquery写出来的tab切换

    当时做这个的时候,当前状态是不规则的,li对应的有3块内容,分别设定不同背景图片,只显示当前的一个背景,鼠标移上去的时候其余2个用background-position: -1000px 0px;来隐 ...

  10. Sublime Text 前端插件推荐

    html/CSS快速编辑 --- Emment HTML CSS JS 美化插件 --- HTML/CSS/JS Prettyfy MarkDown 预览 --- MarkDown Preview J ...