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

If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).

The replacement must be in-place and use only constant extra memory.

Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.

1,2,3 → 1,3,2

3,2,1 → 1,2,3

1,1,5 → 1,5,1

class Solution {
public void nextPermutation(int[] nums) {
int i = nums.length - 2;
while(i >= 0 && nums[i+1] <= nums[i]) i--;
if(i >= 0){
int j = nums.length - 1;
while(j >= 0 && nums[j] <= nums[i]) j--;
swap(nums, i , j);
}
reverse(nums , i+1);
}
private void reverse(int[] nums, int start){
int i = start, j = nums.length - 1;
while(i < j){
swap(nums, i, j);
i++;
j--;
}
}
private void swap(int[] nums, int i, int j){
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
}

LeetCode 31. Next Permutation【Medium】的更多相关文章

  1. Java for LeetCode 207 Course Schedule【Medium】

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  2. [array] leetcode - 31. Next Permutation - Medium

    leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...

  3. LeetCode:按序打印【1114】

    LeetCode:按序打印[1114] 题目描述 我们提供了一个类: 1 2 3 4 5 public class Foo {   public void one() { print("on ...

  4. LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]

    LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...

  5. LeetCode:课程表II【210】

    LeetCode:课程表II[210] 题目描述 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一 ...

  6. LeetCode:累加数【306】

    LeetCode:累加数[306] 题目描述 累加数是一个字符串,组成它的数字可以形成累加序列. 一个有效的累加序列必须至少包含 3 个数.除了最开始的两个数以外,字符串中的其他数都等于它之前两个数相 ...

  7. LeetCode:二进制手表【401】

    LeetCode:二进制手表[401] 题目描述 二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59). 每个 LED 代表一个 0 或 1,最低位在右 ...

  8. LeetCode:翻转二叉树【226】

    LeetCode:翻转二叉树[226] 题目描述 翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 题目 ...

  9. LeetCode:棒球比赛【682】

    LeetCode:棒球比赛[682] 题目描述 你现在是棒球比赛记录员.给定一个字符串列表,每个字符串可以是以下四种类型之一:1.整数(一轮的得分):直接表示您在本轮中获得的积分数.2. " ...

随机推荐

  1. 其它课程中的python---5、Pandas处理数据和读取数据

    其它课程中的python---5.Pandas处理数据和读取数据 一.总结 一句话总结: 记常用和特例:慢慢慢慢的就熟了,不用太着急,慢慢来 库的使用都很简单:就是库的常用函数就这几个,后面用的时候学 ...

  2. (16)centos7 日志文件

    常见日志文件 开机启动日志,只会记录本次信息 /var/log/boot.log 计划任务日志 /var/log/cron 开机内核检测信息 /var/log/dmesg 账号登录信息 /var/lo ...

  3. 解决spring boot中普通类中使用service为null 的方法

    我使用的是springboot+mybatisplus +mysql1.创建一个SpringUtil工具类 import org.springframework.beans.BeansExceptio ...

  4. jeecg 实现lhgDialog窗口传值

    需要在jeecg中的dialog弹框往调用的窗口赋值. 定义内容页调用窗体实例对象接口 var windowapi = frameElement.api ; var W = windowapi.ope ...

  5. 2、什么是session?

    session 什么是Session?Session什么时候产生?    Session:在计算机中,尤其是在网络应用中,称为“会话控制”.Session 对象存储特定用户会话所需的属性及配置信息.这 ...

  6. 前端(二)—— CSS的引入方式、长度与颜色单位、常用样式、选择器

    CSS的引入方式.长度与颜色单位.常用样式.选择器 一.CSS的三种引入方式 1.行间式 <!doctype html> <html> <head> <met ...

  7. OpenFileDialog 打开图片存储到电脑本地上

    替换图片 private void btnSkin_Click(object sender, RoutedEventArgs e) { string fName; OpenFileDialog ofd ...

  8. 第五记 JDBC

    了解JDBC JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API(API:Application Program Inter ...

  9. 32-Ubuntu-用户权限-03-修改文件权限

    chmod 简介 chmod可以修改用户或组对文件或目录的权限. 命令格式如下: chmod +/-rwx 文件名/目录名 修改文件权限 例:demo.txt 1.增加权限 例:增加demo.txt的 ...

  10. C语言指针和数组

    #include <stdio.h> int main() { /********************************************* * * 指针和数组: * 定义 ...