题目链接

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. kubeadm 线上集群部署(二) K8S Master集群安装以及工作节点的部署

    PS:所有机器主机名请提前设置好 在上一篇,ETCD集群我们已经搭建成功了,下面我们需要搭建master相关组件,apiverser需要与etcd通信并操作 1.配置证书 将etcd证书上传到mast ...

  2. route命令详情

    基础命令学习目录首页 原文链接:https://www.cnblogs.com/lpfuture/p/5857738.html 考试题一:linux下如何添加路由(百度面试题) 以上是原题,老男孩老师 ...

  3. 虚拟机搭建Hadoop集群

    安装包准备 操作系统:ubuntu-16.04.3-desktop-amd64.iso 软件包:VirtualBox 安装包:hadoop-3.0.0.tar.gz,jdk-8u161-linux-x ...

  4. Eclipse各版本分析比较

    Eclipse最初是由IBM公司开发的替代商业软件Visual Age for Java的下一代IDE开发环境,2001年11月贡献给开源社区,现在它由非营利软件供应商联盟Eclipse基金会. Ec ...

  5. 常用DB2命令

    建库 db2 territory CN on 建库到指定位置 db2 create database OADB on D: using codeset GBK territory CN 列出所有数据库 ...

  6. PSP Daily软件beta版本——基于spec评论

    题目要求: 每个小组评论其他小组beta发布作品的软件功能说明书. 试用(并截图)所有其他小组的beta作品,与软件功能说明书对比,评论beta作品对软件功能说明书的实现. 根据软件功能说明书,测试所 ...

  7. (第九周)Beta-1阶段成员贡献分

    项目名:食物链教学工具 组名:奋斗吧兄弟 组长:黄兴 组员:李俞寰.杜桥.栾骄阳.王东涵 个人贡献分=基础分+表现分 基础分=5*5*0.5/5=2.5 成员得分如下: 成员 基础分 表现分 个人贡献 ...

  8. 软工1816 · Beta冲刺(5/7)

    团队信息 队名:爸爸饿了 组长博客:here 作业博客:here 组员情况 组员1(组长):王彬 过去两天完成了哪些任务 推进后端完成安卓端接口的开发 在测试中发现返回地图接口存在错误(待修复) 推进 ...

  9. 【贪心算法】POJ-3190 区间问题

    一.题目 Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one wil ...

  10. JQuery TextArea的取值与赋值问题---(textarea中回车清空问题)——个人转载整理

    JQuery TextArea的取值与赋值问题---(textarea中回车清空问题) JQuery TextArea的取值与赋值问题 首先,说明这不是一个简单的问题! 先说取值: $("# ...