Permutations II

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

首先分析一下与Permutations有何差异。

记当前位置为start,当前排列数组为cur

1、cur[start]与cur[start]值相同的元素交换位置会产生大量重复。

如:1,3,2,1

两个1互换位置之后,后续的所有排列都是重复的。

2、cur[start]与其他相同值的元素多次交换位置会产生大量重复。

如:1,2,3,2

1与两个2互换位置后,后续的所有排列都是重复的。

因此改变在于:

对于同一个值,只交换一次,否则跳过。

为了保证这一点,必须对cur数组start位置之后的元素排序,这样可以跳过重复元素。

若不排序会产生如下问题:

0,0,1,9 --> 9,0,1,0

0就不连续了,无法进行判断去重,结果又会产生重复。

class Solution {
public:
vector<vector<int> > permuteUnique(vector<int> &num) {
vector<vector<int> > ret;
Helper(ret, num, );
return ret;
}
void Helper(vector<vector<int> >& ret, vector<int> num, int pos)
{
if(pos == num.size()-)
ret.push_back(num);
else
{
sort(num.begin()+pos, num.end());
for(int i = pos; i < num.size(); i ++)
{
if(i != pos && num[i] == num[i-])
continue;
swap(num[pos], num[i]);
Helper(ret, num, pos+);
swap(num[pos], num[i]);
}
}
}
};

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

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

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

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

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

  3. 【LeetCode】047. Permutations II

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

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

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

  5. 【LeetCode】90. Subsets II 解题报告(Python & C++)

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

  6. 【LeetCode】Two Sum II - Input array is sorted

    [Description] Given an array of integers that is already sorted in ascending order, find two numbers ...

  7. 【LeetCode】基本计算器II

    [问题]实现一个基本的计算器来计算一个简单的字符串表达式的值.字符串表达式仅包含非负整数,+, - ,*,/ 四种运算符和空格  .整数除法仅保留整数部分. 输入: "3+2*2" ...

  8. 【LeetCode 】N皇后II

    [问题]n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法.给定一个整数 n,返回 n 皇后不同的解决方案的数量. 示例: ...

  9. 【LeetCode】跳跃游戏II

    [问题]给定一个非负整数数组,你最初位于数组的第一个位置.数组中的每个元素代表你在该位置可以跳跃的最大长度.你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [,,,,] 输出: ...

随机推荐

  1. 关于Themleaf学习总结

    此篇记录学习Themleaf测试的相关用例: study01 Thymeleaf 的HelloWorld级别的例子 简单介绍Thymeleaf的工作流程 study02 使用spring.thymel ...

  2. redis.clients.jedis.exceptions.JedisDataException: WRONGTYPE Operation against a key holding the wrong kind of value

    错误原因: 因为redis中已经存在了相同的key, 而且key对应的值类型并不是Set,而是SortSet(改动前):再调用smembers时,抛出此错误. 解决方法: 将原来的的key给Del掉, ...

  3. phpExcel导出文件时内存溢出的问题

    在使用PHPExcel导出文件时,经常会因为文件过大导致PHP内存溢出报错,为了解决这个问题,可以使用PHPExcel提供的参数进行优化.这里说的Excel文件过大并不一定是文件大小,更关键的在于文件 ...

  4. 启动Memcached报错:/usr/local/memcached/bin/memcached: error while loading shared libraries: libevent-2.1.so.6: cannot open shared object file: No such file or directory

    1.查找文件放在哪里 sudo find / -name libevent-2.1.so.6 发现放在/usr/local/lib/libevent-2.1.so.6下. 2.创建软链接 sudo l ...

  5. otl翻译(11) -- OTL的迭代器

    OTL stream read iterator 这个类是一个像传统的JDBC中的getter()操作一样扩展了OTL流的模板类.它现在还不支持UNICODE字符集.它对otl_refcur_stre ...

  6. 网页IE轻松调用VLC播放器实现监控(组件+方法大全)【转】

    公司突发奇想,要把刚买回来的网络监控机用自己内部网站在线监控. 作为网站的开发员,我接下了这个任务. 网络上有很多资料参与,但是都不全都不尽人意,最后经过多次的不同关键字的查找和测试,总算让我成功了. ...

  7. Convert Sorted Array to Binary Search Tree leetcode java

    题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...

  8. Windows服务器PHPstudy配置安装微擎教程

    此教程只适于无环境的新服务器,有环境请勿安装用电脑上面的远程桌面连接登陆服务器.1,下载微擎安装包,下载PHPstudy,下载V9运行库.安装PHPstudy.安装V9运行库.准备好3个. <i ...

  9. ftp-ftp权限

    在服务器上创建ftp站点时勾选的是读写权限对所有的用户开放,但是发现有些用户还是只能读取不能写入,后来发现是因为ftp指向的文件夹本身的权限没有打开导致的,解决办法是,设置ftp指向的文件夹的权限为u ...

  10. [Javascript]1. Improve you speed! Loop optimaztion

    /** Improve you loop code */ var treasureChest = { goldCoins: 10000, magicalItem : "Crown of Sp ...