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;
}
};

小结
- 利用Next Permutation - LeetCode的函数来求下一个全排列
Permutations II - LeetCode的更多相关文章
- Permutations II ——LeetCode
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- Permutations II leetcode java
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- leetcode Permutations II 无重全排列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...
- [Leetcode][Python]47: Permutations II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...
- LeetCode解题报告—— Permutations & Permutations II & Rotate Image
1. Permutations Given a collection of distinct numbers, return all possible permutations. For exampl ...
- Leetcode之回溯法专题-47. 全排列 II(Permutations II)
Leetcode之回溯法专题-47. 全排列 II(Permutations II) 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2] ...
- 【leetcode】Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- LeetCode:Permutations, Permutations II(求全排列)
Permutations Given a collection of numbers, return all possible permutations. For example, [1,2,3] h ...
- leetcode总结:permutations, permutations II, next permutation, permutation sequence
Next Permutation: Implement next permutation, which rearranges numbers into the lexicographically ne ...
随机推荐
- logout命令详解
基础命令学习目录首页 logout指令让用户退出系统,其功能和login指令相互对应.语法 logout
- react-native 常规操作
1. 关闭xcode打开模拟器的快捷键 , 等常规操作 https://www.jianshu.com/p/f6723f3406b7
- Daily Scrum (2015/11/1)
今天晚上我们照例召开了每周末的小组例会,主要总结本周的工作和讨论下一周的工作. 首先是本周的一些主要工作: 1.进行了代码的修改和完善. 2.开始进行服务器配置和UI. 3.学习借鉴了nutch爬虫的 ...
- [buaa-SE-2017]个人作业-week3
个人作业-week3:案例分析 分析产品:Bing词典 Part1:调研&评测 1.软件评测和Bug汇报 这次我选择Bing词典的原因是在于,首先我使用过的词典软件较多,平台涵盖PC端.网站. ...
- 项目Beta冲刺团队随笔集
博客集如下: Beta冲刺Day1:第一天冲刺记录 Beta冲刺Day2:第二天冲刺记录 Beta冲刺Day3:第三天冲刺记录 Beta冲刺Day4:第四天冲刺记录 Beta冲刺Day5:第五天冲刺记 ...
- 使用Git进行代码管理心得------------个人练习
一.在github.com上的操作 今天我们实践课程学习了用Git进行代码版本,使用github进行代码托管,我和队友在官网上创建了自己的Organization,将Auto CS fork到了小 ...
- DPDK实例程序:testpmd
用户手册:https://doc.dpdk.org/guides/testpmd_app_ug/index.html 还不错的入门:http://syswift.com/188.html 我的运行情况 ...
- Beta阶段团队项目开发篇章3
例会时间 2016.12.6晚 例会照片 个人工作 上阶段任务验收 中英文切换功能已经实现,调查结果分析已经完成,博客基本撰写完成,在征求其他组员意见后发布.任务基本完成. 任务分配 组员 任务内容 ...
- 10th 规格说明书练习——吉林一日游
活动规格说明书 吉林市一日游 版本:1.0 编订:王东涵 团队:2016级计算机技术全体同学 日期:2016-11-20 目录 1.引言 1.1 编写目的 1.2 背景 1.3 定义 1.4 参考资料 ...
- 关于VS2005中C#代码用F12转到定义时,总是显示从元数据的问题
元数据是:NET 程序集中的标记信息. 是在代码中选择了转到定义时候给定位的吧.因为没有找到源代码,VS通过反射读取元数据中的信息生成了那个. 解决方法: 1. 要把项目先添加到解决方案中. 2. 再 ...