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

  1. class Solution {
  2. public void nextPermutation(int[] nums) {
  3. int i = nums.length - 2;
  4. while(i >= 0 && nums[i+1] <= nums[i]) i--;
  5. if(i >= 0){
  6. int j = nums.length - 1;
  7. while(j >= 0 && nums[j] <= nums[i]) j--;
  8. swap(nums, i , j);
  9. }
  10. reverse(nums , i+1);
  11. }
  12. private void reverse(int[] nums, int start){
  13. int i = start, j = nums.length - 1;
  14. while(i < j){
  15. swap(nums, i, j);
  16. i++;
  17. j--;
  18. }
  19. }
  20. private void swap(int[] nums, int i, int j){
  21. int t = nums[i];
  22. nums[i] = nums[j];
  23. nums[j] = t;
  24. }
  25. }

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. Android逆向之smali语法宝典

    0x01.前言 Android采用的是java语言进行开发,但是Android系统有自己的虚拟机Dalvik,代码编译最终不是采用的java的class,而是使用的smali.我们反编译得到的代码,j ...

  2. 9. Python函数

    函数 函数能提高应用的模块性,和代码的重复利用率.定义一个函数比较简单,但是需要遵循以下几点规则: 函数代码块以 def 关键词开头,后接函数标识符名称和圆括号 (). 任何传入参数和自变量必须放在圆 ...

  3. 18、Page Object 设计模式

    Page Object 设计模式的优点如下: 减少代码的重复. 提高测试用例的可读性. 提高测试用例的可维护性, 特别是针对 UI 频繁变化的项目. 当你针对网页编写测试时,你需要引用该网页中的元素, ...

  4. 2、Locust压力测试 实战

    创建测试脚本 创建Test()类继承TaskSet类 创建beigong() 方法表示一个行为,访问北弓官网首页.用@task() 装饰该方法为一个任务.1表示一个Locust实例被挑选执行的权重,数 ...

  5. 【react】---Hooks的基本使用---【巷子】

    一.react-hooks概念 React中一切皆为组件,React中组件分为类组件和函数组件,在React中如果需要记录一个组件的状态的时候,那么这个组件必须是类组件.那么能否让函数组件拥有类组件的 ...

  6. 错误 1 error C4996: 'getcwd': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getcwd. See online help for details.

    解决办法: 属性>C/C++>预处理定义>编辑>添加_CRT_NONSTDC_NO_DEPRECATE>应用

  7. 020_JUC

    JUC Java.util.Concurrent 并发包 池的顶级接口 Executor 子接口 ExecutorService 工具类 Executors(Collections.Arrays .. ...

  8. Pandas异常值处理

    import pandas as pd #生成异常数据 df=pd.DataFrame({'col1':[1,120,3,5,2,12,13], 'col2':[12,17,31,53,22,32,4 ...

  9. 用户态和内核态&操作系统

    用户态和内核态 内核态:cpu可以访问内存的所有数据,包括外围设备,例如硬盘,网卡,cpu也可以将自己从一个程序切换到另一个程序. 用户态:只能受限的访问内存,且不允许访问外围设备,占用cpu的能力被 ...

  10. 13、如何拆分含有多种分隔符的字符串 14、如何判断字符串a是否以字符串b开头或结尾 15、如何调整字符串中文本的格式 16、如何将多个小字符串拼接成一个大的字符串

    13.如何拆分含有多种分隔符的字符串 import re s = "23:41:2314\1234#sdf\23;" print(re.split(r'[#:\;]+',s))   ...