52-下一个排列

给定一个整数数组来表示排列,找出其之后的一个排列。

注意事项

排列中可能包含重复的整数

样例

给出排列[1,3,2,3],其下一个排列是[1,3,3,2]

给出排列[4,3,2,1],其下一个排列是[1,2,3,4]

标签

排列 LintCode 版权所有

思路

从后往前找,找到第一对(i,j),使得 nums[i] < num[j] ,然后将两者交换后,后面部分排序即可。

code

class Solution {
public:
/**
* @param nums: An array of integers
* @return: An array of integers that's next permuation
*/
vector<int> nextPermutation(vector<int> &nums) {
// write your code here
int size = nums.size(), i = 0, j = 0;
if(size == 0) {
return vector<int> ();
} for(i=size-1; i>=0; i--) {
for(j=size-1; j>i; j--) {
if(nums[i]<nums[j]) {
swap(nums[i],nums[j]);
sort(nums.begin()+i+1,nums.end());
return nums;
}
}
}
sort(nums.begin(),nums.end());
return nums;
}
};

lintcode-52-下一个排列的更多相关文章

  1. dfs 之 下一个排列

    52. 下一个排列 中文English 给定一个整数数组来表示排列,找出其之后的一个排列. Example 例1: 输入:[1] 输出:[1] 例2: 输入:[1,3,2,3] 输出:[1,3,3,2 ...

  2. lintcode:next permutation下一个排列

    题目 下一个排列 给定一个整数数组来表示排列,找出其之后的一个排列. 样例 给出排列[1,3,2,3],其下一个排列是[1,3,3,2] 给出排列[4,3,2,1],其下一个排列是[1,2,3,4] ...

  3. LinkCode 下一个排列、上一个排列

    http://www.lintcode.com/zh-cn/problem/next-permutation-ii/# 原题 给定一个若干整数的排列,给出按正数大小进行字典序从小到大排序后的下一个排列 ...

  4. [LeetCode] Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  5. C++构造 下一个排列 的函数

    今天围观刘汝佳神犇的白书发现了一个好用的函数: next_permutation(); 可以用于可重, 或者不可重集, 寻找下一个排列. 时间复杂度尚不明. //适用于不可重和可重集的排列. # in ...

  6. Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  7. [Swift]LeetCode31. 下一个排列 | Next Permutation

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  8. LeetCode(31): 下一个排列

    Medium! 题目描述: (请仔细读题) 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列) ...

  9. 【LeetCode每天一题】Next Permutation(下一个排列)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  10. leetcode31题:下一个排列

    实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改,只允许使用额外常数空间. ...

随机推荐

  1. Struts2后期(这框架目前正处于淘汰状态)

    Struts2第三天 课程回顾:Struts2框架的第二天 1. Servlet的API * ActionContext对象 * ServletActionContext对象 2. 结构类型的跳转 * ...

  2. spring入门(三) 使用spring mvc

    1.建立project / module 新建空的project:springMvcStudy 新建module:type maven-webapp,名字mvcStudy 2.为module设置Sou ...

  3. TcpServer 使用简介

    1.简介 1) Poco 的 TcpServer 是一个多线程的 Tcp 服务器. 服务器使用 ServerSocket(Poco 的一个用于初始化服务器的socket的类) 来接收链接.Server ...

  4. 转:Java子线程中的异常处理(通用)

    引自:https://www.cnblogs.com/yangfanexp/p/7594557.html 在普通的单线程程序中,捕获异常只需要通过try ... catch ... finally . ...

  5. js实现监听浏览器窗口大小改变事件

    window.onresize = function(){   }

  6. Hadoop参数调优

    转自:http://blog.sina.com.cn/s/blog_6a67b5c50100vop9.html dfs.block.size 决定HDFS文件block数量的多少(文件个数),它会间接 ...

  7. C语言结构体的学习,以及gdb的调式

    #include <stdio.h> #include <string.h> #define format "%d\n%s\n%f\n%f\n%f\n" t ...

  8. 函数:引用file类对象及io类对象作为参数打印文本及显示文本

    #include <iostream> #include <fstream> #include <cstdlib> using namespace std; voi ...

  9. 005---基于UDP的套接字

    基于UDP的套接字 udp不同于tcp协议:不需要经过三次握手.四次挥手.直接发送数据就行. 服务端 import socket ip_port = ('127.0.0.1', 8001) buffe ...

  10. node 动态页面渲染

    代码: 'use strict' const express = require('express'); const consoldiate = require('consolidate'); con ...