【一天一道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 ...
随机推荐
- ACM pie
我的生日快到了,传统上我正在做馅饼.不只是一个馅饼,不,我有N个,各种口味和各种尺寸. 数量为F我的朋友会来到我的聚会,每个人都得到一个馅饼. 这应该是一块馅饼,而不是几个小块,因为看起来很乱.这一块 ...
- Node.js 逐行读取
逐行读取 稳定性: 2 - 不稳定 使用 require('readline'),可以使用这个模块.逐行读取(Readline)可以逐行读取流(比如process.stdin) 一旦你开启了这个模块, ...
- Android 学习笔记二 自定义按钮形状 颜色 点击渐变
问题:自定义按钮的颜色 形状弧度 渐变效果 1.新建自定义属性button_login.xml (借鉴某大神) <?xml version="1.0" encoding=& ...
- 安卓高级 Android图片缓存之初识Glide
前言: 前面总结学习了图片的使用以及Lru算法,今天来学习一下比较优秀的图片缓存开源框架.技术本身就要不断的更迭,从最初的自己使用SoftReference实现自己的图片缓存,到后来做电商项目自己的实 ...
- 修改hosts立刻生效不必重启
有时我们会通过修改Hosts文件(路径为系统盘:\WINDOWS\system32\drivers\etc\hosts),在修改并保存Hosts文件后需要重启才能使设置生效. 这时可以打开命令提示符 ...
- Markdown语法及SublimeText下使用技巧
Markdown语法及SublimeText下使用技巧 0.缘起 最近因为一直在学习Sublime Text,所以也就顺便试用了一下ST对Markdown的支持.正好CSDN正在大力宣传新上线的Mar ...
- 代理IP爬取,计算,发放自动化系统
IoC Python端 MySQL端 PHP端 怎么使用 这学期有一门课叫<物联网与云计算>,于是我就做了一个大作业,实现的是对代理IP的爬取,计算推荐,发放给用户等任务的的自动化系统.由 ...
- ROS机器人程序设计(原书第2版)补充资料 (贰) 第二章 ROS系统架构及概念
ROS机器人程序设计(原书第2版)补充资料 (贰) 第二章 ROS系统架构及概念 书中,大部分出现hydro的地方,直接替换为indigo或jade或kinetic,即可在对应版本中使用. 由于工作事 ...
- 关于[[NSNotificationCenter defaultCenter] addObserver不remove后续又收到通知crash问题
今天试了一个小demo,测出一个现象,同步出来:object 作为 observer 监听了通知 A,然后 object 中途被释放执行了dealloc,随后app发出这个通知 A:iOS 6.iOS ...
- make、make clean、make install、make uninstall、make dist、make distcheck和make distclean
Makefile在符合GNU Makefiel惯例的Makefile中,包含了一些基本的预先定义的操作:make根据Makefile编译源代码,连接,生成目标文件,可执行文件.make clean清除 ...