【一天一道LeetCode】#47. Permutations II
一天一道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的更多相关文章
- leetCode 47.Permutations II (排列组合II) 解题思路和方法
Permutations II Given a collection of numbers that might contain duplicates, return all possible un ...
- [LeetCode] 47. Permutations II 全排列 II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] 47. Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [leetcode] 47. Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode 47 Permutations II(全排列)
题目链接: https://leetcode.com/problems/permutations-ii/?tab=Description 给出数组,数组中的元素可能有重复,求出所有的全排列 使 ...
- [Leetcode][Python]47: Permutations II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...
- 【LeetCode】47. Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- [LeetCode] 47. 全排列 II
题目链接 : https://leetcode-cn.com/problems/permutations-ii/ 题目描述: 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [ ...
- 【leetcode】Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
随机推荐
- Dynamics CRM2016 Web API之删除单个查找字段值
之前的博文中有介绍过,Web Api中的一个删除单个属性的Api但没提供查找字段的删除方法,本篇补充上,这里给出的示例代码是C#的(主要看url的拼接),看下url中最后的/$ref,这个标示表明了当 ...
- sublime text 2 解决错误 [Decode error - output not utf-8]
以win 10 为例, 找到文件C:\Users\xxzx\AppData\Roaming\Sublime Text 2\Packages\Python\Python.sublime-build 添加 ...
- 【SSH系列】spring中为什么要使用IOC
开篇前言 在前面的博文中,小编主要简单的介绍了spring的入门知识,随着学习的深入,我们知道spring最核心的两大技术,IOC和AOP,这两个技术也是spring最耀眼的地方,在后续的博文中小编将 ...
- 如何在控制台切换Xcode的版本
打开控制台,输入 xcode-select -p 你可以看到当前Xcode所使用的版本路径,比如本猫的输出为: /Applications/Xcode-beta.app/Contents/Develo ...
- JVM概述
JVM是什么 JVM全称是Java Virtual Machine(java虚拟机).它之所以被称之为是"虚拟"的,就是因为它仅仅是由一个规范来定义的抽象计算机.我们平时经常使用的 ...
- Findbugs异常总汇
FindBugs是基于Bug Patterns概念,查找javabytecode(.class文件)中的潜在bug,主要检查bytecode中的bug patterns,如NullPoint空指针检查 ...
- [Vim]vim使用笔记--分屏操作
我们经常要打开多个文件,不同的窗口操作多个文件,分屏就很好用了. 1 命令模式下: :new,新建文件并分屏, 快捷键,Ctrl+W,然后马上按n键 :spilt 水平分屏,将当前屏分为两个,水平的. ...
- 【移动开发】EditText输入字数限制总结(包括中文输入内存溢出的解决方法)
限定EditText输入个数的解决方案很多,但是一般主要考虑两点,也就是处理两件事:(1)不同语言字符(英文.中文等)处理方式(2)输入字符达到数目后,是否仍然允许用户输入 第一点,涉及的东东其实蛮多 ...
- Vibrator控制手机震动
Vibrator控制手机震动 效果图 源码 下载地址(Android Studio工程):http://download.csdn.net/detail/q4878802/9049755 添加权限 & ...
- Android初级教程:如何自定义一个状态选择器
有这样一种场景:点击一下某个按钮或者图片(view),改变了样式(一般改变背景颜色).这个时候一种解决方案,可能就是状态选择器.接下来就介绍如何实现状态选择器: 步骤: 一.新建这样的文件夹:res/ ...