题目链接

Permutations II - LeetCode

注意点

  • 不确定有几种排列

解法

解法一:因为有重复的数字所以排列的个数不确定几个,一直生成新的排列直到和原始的数列相同为止

class Solution {
public:
vector<int> nextPermutation(vector<int> nums) {
int n = nums.size(),i = n-2,j = n-1;
while(i >= 0 && nums[i] >= nums[i+1]) i--;
if(i >= 0)
{
while(nums[j] <= nums[i]) j--;
swap(nums[i],nums[j]);
}
reverse(nums.begin()+i+1,nums.end());
return nums;
}
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int>> ret;
ret.push_back(nums);
vector<int> temp = nextPermutation(nums);
while(temp != nums)
{
ret.push_back(temp);
temp = nextPermutation(temp);
}
return ret;
}
};

小结

Permutations II - LeetCode的更多相关文章

  1. Permutations II ——LeetCode

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

  2. Permutations II leetcode java

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

  3. leetcode Permutations II 无重全排列

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...

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

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

  5. LeetCode解题报告—— Permutations & Permutations II & Rotate Image

    1. Permutations Given a collection of distinct numbers, return all possible permutations. For exampl ...

  6. Leetcode之回溯法专题-47. 全排列 II(Permutations II)

    Leetcode之回溯法专题-47. 全排列 II(Permutations II) 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2] ...

  7. 【leetcode】Permutations II

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

  8. LeetCode:Permutations, Permutations II(求全排列)

    Permutations Given a collection of numbers, return all possible permutations. For example, [1,2,3] h ...

  9. leetcode总结:permutations, permutations II, next permutation, permutation sequence

    Next Permutation: Implement next permutation, which rearranges numbers into the lexicographically ne ...

随机推荐

  1. Halcon中循环的相关算子

    条件<condition> ,<condition> 内为计算成an integer or boolean value的表达式. 表达式的值1则条件为真,否则为假. 1.if( ...

  2. TPO-21 C2 Which elective courses to take

    /* 加粗:语音部分 * 红色:单词部分 * 斜体:语法部分 * 下划线:信号词/句 */ 第 1 段 1.Listen to a conversation between a student and ...

  3. Xshell6远程访问linux及Xftp6远程针对linux系统中文件操作(附图文详解)

    1.首先我们需要先做好前期准备工作,需要到XManager6官网上将Xshell及Xftp下载并安装,安装过程一直下一步就好了.这里是其官网:http://www.xshellcn.com/.安装完成 ...

  4. OpenGL学习笔记(4) GLM库的使用

    OpenGL和DirextX不一样,没有内置的数学库,于是我们需要找一个第三方库,按照LearnOpenGL的教程我们使用GLM库,可以到他们的官网下载 glm常用的数据类型 vec2 二维向量 ve ...

  5. 如何获取c:forEach里面点击时候的值

    1.c:forEach遍历输出 <c:forEach items="${data}" var="item" > <a onclick=&quo ...

  6. Aria2 Linux 完整安装及使用教程

    Aria2 嘛,主要是用来离线下载,功能强大,支持 http/https 直链.ftp.电驴.磁力链接等等,且可以跨平台使用,配合网页端操作,简直是一代下载神器. 安装 Debian/Ubuntu: ...

  7. 关于如何使用dubbo管理控制台的一些感想

    1.起因 ​因java项目需要准备安装一个dubbo-admin管理后台研究使用,无奈github上并没有看到dubbo-admin的目录着实让人着急.百度引擎上一些文章也不靠谱!真是浪费时间!所以又 ...

  8. 互评Alpha版本—SkyHunter

    1.根据NABCD评论作品:   N(Need,需求):飞机大战题材的游戏对80,90后的人来说算是童年的记忆,可以在闲暇之余打开电脑玩一会儿.但是面向初中生,高中生的话这种PC小游戏可能不会那么适合 ...

  9. Notes of Daily Scrum Meeting(11.11)

    Notes of Daily Scrum Meeting(11.11) 今天是11月11号光棍节,不知道大家的购物热情被点燃没有,有没有买到自己心仪的东西.额,今天我们的团队任务进度和昨天差不多, 每 ...

  10. Task 6.2冲刺会议五 /2015-5-18

    今天继续深入的看了看服务器的内容,修改了昨天的代码,发现网络编程还是很好玩的,感觉他的代码比平常写的更有趣一点,另外登陆界面也稍微看了一点.明天准备把登陆界面完善一下.