一天一道LeetCode系列

(一)题目

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

(二)解题

求全排列数。具体思路可以参考【一天一道LeetCode】#46. Permutations这篇博客。

不同之处:上一题没有重复的数字,这一题有重复的数字



class Solution {

public:

    vector<vector<int>> permute(vector<int>& nums) {

        vector<vector<int>> ret;

        sort(nums.begin(),nums.end());

        do{

            ret.push_back(nums);

        }while(nextpermute(nums));

        return ret;

    }

    bool nextpermute(vector<int>& nums)

    {

        int i = nums.size() -2;

        while(i>=0 && nums[i] >= nums[i+1]) i--;//考虑到有相等的情况,这里需要找到第一个破坏非降序的数

        int j = nums.size()-1;

        while(j>=0 && nums[j] <= nums[i]) j--;//找到第一个大于i的数

        if(i>=0)

        {

            swap(nums[i],nums[j]);//交换i和j

            reverse(nums.begin()+i+1,nums.end());//将i以后的数反转

            return true;//如果还存在下一个全排列数就返回true

        }

        return false;//如果数组已经为倒序了就说明没有下一个了,返回false

    }

};

【一天一道LeetCode】#47. Permutations II的更多相关文章

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

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

  2. [LeetCode] 47. Permutations II 全排列 II

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

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

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

  4. [leetcode] 47. Permutations II

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

  5. LeetCode 47 Permutations II(全排列)

    题目链接: https://leetcode.com/problems/permutations-ii/?tab=Description   给出数组,数组中的元素可能有重复,求出所有的全排列   使 ...

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

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

  7. 【LeetCode】47. Permutations II

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

  8. [LeetCode] 47. 全排列 II

    题目链接 : https://leetcode-cn.com/problems/permutations-ii/ 题目描述: 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [ ...

  9. 【leetcode】Permutations II

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

随机推荐

  1. Linux下MySQL 数据库的基本操作

    1. 创建数据库相关命令: 首先,下载MySQL相关软件包:aptitude install mysql-server/mysql-client MySQL中的root用户类似于Linux下的root ...

  2. Java实验链接

    第1次实验 课堂实验内容:Java入门+Eclipse+PTA+Git+博客 实验任务书:第01次试验(安装JDK.编辑器.编写出第一个Java程序).pdf Eclipse简明教程(by郑如滨).p ...

  3. 分布式服务框架Dubbo

    随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进. 单一应用架构 当网站流量很小时,只需一个应用, ...

  4. Android服务——Service

    服务 Service 是一个可以在后台执行长时间运行操作而不使用用户界面的应用组件.服务可由其他应用组件启动,而且即使用户切换到其他应用,服务仍将在后台继续运行. 此外,组件可以绑定到服务,以与之进行 ...

  5. 使用Spring Boot开发Web项目(二)之添加HTTPS支持

    上篇博客使用Spring Boot开发Web项目我们简单介绍了使用如何使用Spring Boot创建一个使用了Thymeleaf模板引擎的Web项目,当然这还远远不够.今天我们再来看看如何给我们的We ...

  6. Lucene查询结果高亮

    检索结果高亮 实现效果: 核心代码 package ucas.ir.lucene; import java.io.File; import java.io.IOException; import ja ...

  7. ELK搭建

    ELK安装 elasticsearch安装 * 下载elasticsearch-5.0.0.tar.gz,并解压. 通过elasticsearch.yml可设置host和port. vim confi ...

  8. FORM界面批量处理-全选框实现

    全选框实现方法多种多样,这里只介绍两种 方法一:触发器式,优点程序简单,缺点颜色单调不突出 1.      在数据块和控制块上分别创建check box 2.      设置check box选中与为 ...

  9. SpriteKit中类似Cocos2D的CCActionSpawn并发方法GroupAction

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 我们知道在Cocos2D中对于并发Action的处理可以使用C ...

  10. SQLite Update 语句(http://www.w3cschool.cc/sqlite/sqlite-update.html)

    SQLite Update 语句 SQLite 的 UPDATE 查询用于修改表中已有的记录.可以使用带有 WHERE 子句的 UPDATE 查询来更新选定行,否则所有的行都会被更新. 语法 带有 W ...