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]
] 思路1.
这题与Permutations的区别在于他允许重复数字,最简单的就是利用1的结果,保存在一个set中,去重复后保存结果,但不够理想。 思路2. 对数组先进行排序,如果有重复数字,则跳过,需要注意的是,因为排序,如果采用引用传递,则会破坏排序,所以使用值传递。当然也可以使用引用传递,但每次传递后,把交换的数据交换回来并且对后序数组再进行排序。
class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int>> result;
sort(nums.begin(),nums.end());
permuteRec( 0, nums, result);
return result;
} private:
void permuteRec( int start, vector<int> nums, vector<vector<int>> &result ){
if( start >= nums.size()){
result.push_back(nums);
return;
}
for( int i = start; i < nums.size(); i++ ){
if( i > start && nums[i] == nums[start]) continue;
swap( nums[start], nums[i] );
permuteRec( start+1, nums, result);
}
}
};

  

LeetCode 【47. Permutations II】的更多相关文章

  1. [Leetcode][Python]47: Permutations II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...

  2. LeetCode OJ 47. Permutations II

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

  3. leetcode 【 Unique Paths II 】 python 实现

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  4. 【LeetCode】47. Permutations II

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

  5. [LeetCode] 47. Permutations II 全排列之二

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  6. leetCode 47.Permutations II (排列组合II) 解题思路和方法

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

  7. leetcode46. Permutations 、47. Permutations II、 剑指offer字符串的排列

    字符串排列和PermutationsII差不多 Permutations第一种解法: 这种方法从0开始遍历,通过visited来存储是否被访问到,level代表每次已经存储了多少个数字 class S ...

  8. 【一天一道LeetCode】#47. Permutations II

    一天一道LeetCode系列 (一)题目 Given a collection of numbers that might contain duplicates, return all possibl ...

  9. 【LeetCode】47. Permutations II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

随机推荐

  1. JSPServlet精华笔记

    一.     JSP (Java Server Pages) JSP是指: ▶    在HTML中嵌入Java脚本代码 ▶    由应用服务器中的JSP引擎来编译和执行嵌入的Java脚本代码 ▶    ...

  2. MFC编程入门之十六(对话框:消息对话框)

    前面几节讲了属性页对话框,我们可以根据所讲内容方便的建立自己的属性页对话框.本节讲解Windows系统中最常用最简单的一类对话框--消息对话框. 我们在使用Windows系统的过程中经常会见到消息对话 ...

  3. neon指令,注意事项

    1. vbic_s8 (int8x8_t a, int8x8_t b) 是  ~(ai & bi),一开始理解成  (~ai )& bi 导致出错 2.uint8x8_t vqshrn ...

  4. php_match/preg_match_all 默认有字符串长度限制

    php_match/preg_match_all 默认有字符串长度限制:52500(或许你的服务器环境是更长,或者更短),当字符串长度大于52500,只能匹配到52500数据,超出的部分会被系统自己截 ...

  5. SPSS数据分析—混合线性模型

    之前介绍过的基于线性模型的方差分析,虽然扩展了方差分析的领域,但是并没有突破方差分析三个原有的假设条件,即正态性.方差齐性和独立性,这其中独立性要求较严格,我们知道方差分析的基本思想其实就是细分,将所 ...

  6. 【接口测试】Jenkins+Ant+Jmeter搭建持续集成的接口测试平台

    参考文档: http://www.cnblogs.com/liuqi/p/5224579.html

  7. 将Python脚本封装成exe可执行文件 转

    将Python脚本封装成exe可执行文件 http://www.cnblogs.com/renzo/archive/2012/01/01/2309260.html  cx_freeze是用来将 Pyt ...

  8. 查看Msi文件内容

    1通过msiexec命令解压msi包 msiexec.exe /a c:\msi\installer.msi /qb targetdir=d:\msi\installer 2 使用Orca查看. Or ...

  9. OLE填充EXCEL 多SHEET

    "1 设置行高 "参数说明:行/列号.行高/列宽.R-行 C-列 FORM row_column USING p_r p_width p_type. CASE p_type. WH ...

  10. Objective-C( Category 分类,非正式协议,分类延展)

    分类: 主要是给原来类增加一些方法;不可以增加成员变量 Person (Person+eat) 非正式协议: 非正式协议就是类别,即凡是NSObject或其子类Foundation框架中的类增加的类别 ...