一天一道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. window环境搭建zookeeper,kafka集群

    为了演示集群的效果,这里准备一台虚拟机(window 7),在虚拟机中搭建了单IP多节点的zookeeper集群(多IP节点的也是同理的),并且在本机(win 7)和虚拟机中都安装了kafka. 前期 ...

  2. C++ substr

    函数文档:http://www.cplusplus.com/reference/string/string/substr/ /* substr(size_t pos = 0,size_t len = ...

  3. 2017-暑假作业-Java语言程序设计

    任务列表 1.学会使用Markdown做笔记 本篇随笔就是使用的Markdown语法.养成做笔记的习惯! 参考资料: 极简MarkDown排版介绍(How to) stackedit:在线Markdo ...

  4. 使用GDB调试STL容器

    GDB中print方法并不能直接打印STL容器中保存的变量,想知道STL容器保存的变量,使用如下办法: 1. 创建文件~/.gdbinit: # # STL GDB evaluators/views/ ...

  5. Oracle常用语句语法汇总

    第一篇  基本操作 --解锁用户   alter user 用户 account unlock; --锁定用户   alter user 用户 account lock; alter user sco ...

  6. 剑指Offer——知识点储备-数据库基础

    剑指Offer--知识点储备-数据库基础 数据库 事务 事务的四个特性(ACID):   原子性(Atomicity).一致性(Consistency).隔离性(Isolation).持久性(Dura ...

  7. Android图表库MPAndroidChart(十)——散点图的孪生兄弟气泡图

    Android图表库MPAndroidChart(十)--散点图的孪生兄弟气泡图 起泡图和散点图如出一辙,但是个人认为要比散点图好看一点,我们来看下实际的演示效果 这个和散点图的实现很相似,我们一起来 ...

  8. ROS机器人程序设计(原书第2版)补充资料 教学大纲

    ROS机器人程序设计(原书第2版) 补充资料 教学大纲 针对该书稍后会补充教学大纲.教案.多媒体课件以及练习题等. <ROS机器人程序设计>课程简介 课程编号:XXXXXX 课程名称:RO ...

  9. SQLite 删除表(http://www.w3cschool.cc/sqlite/sqlite-drop-table.html)

    SQLite 删除表 SQLite 的 DROP TABLE 语句用来删除表定义及其所有相关数据.索引.触发器.约束和该表的权限规范. 使用此命令时要特别注意,因为一旦一个表被删除,表中所有信息也将永 ...

  10. Gazebo與ROS版本說明

    使用哪种ROS / Gazebo版本的组合 介绍 本文档提供了有关将不同版本的ROS与不同版本的Gazebo结合使用的选项的概述.建议在安装Gazebo ROS包装之前阅读它.重要!简单的分析,快速和 ...