LeetCode 31. Next Permutation【Medium】
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】的更多相关文章
- 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 ...
- [array] leetcode - 31. Next Permutation - Medium
leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...
- LeetCode:按序打印【1114】
LeetCode:按序打印[1114] 题目描述 我们提供了一个类: 1 2 3 4 5 public class Foo { public void one() { print("on ...
- LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]
LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...
- LeetCode:课程表II【210】
LeetCode:课程表II[210] 题目描述 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一 ...
- LeetCode:累加数【306】
LeetCode:累加数[306] 题目描述 累加数是一个字符串,组成它的数字可以形成累加序列. 一个有效的累加序列必须至少包含 3 个数.除了最开始的两个数以外,字符串中的其他数都等于它之前两个数相 ...
- LeetCode:二进制手表【401】
LeetCode:二进制手表[401] 题目描述 二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59). 每个 LED 代表一个 0 或 1,最低位在右 ...
- LeetCode:翻转二叉树【226】
LeetCode:翻转二叉树[226] 题目描述 翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 题目 ...
- LeetCode:棒球比赛【682】
LeetCode:棒球比赛[682] 题目描述 你现在是棒球比赛记录员.给定一个字符串列表,每个字符串可以是以下四种类型之一:1.整数(一轮的得分):直接表示您在本轮中获得的积分数.2. " ...
随机推荐
- CSS:CSS 伪元素
ylbtech-CSS:CSS 伪元素 1.返回顶部 1. CSS 伪元素 CSS伪元素是用来添加一些选择器的特殊效果. 语法 伪元素的语法: selector:pseudo-element {pro ...
- input checkbox 禁止选中/修改
input checkbox 禁止选中 <input type="checkbox" onclick="return false;" /> inp ...
- Collection Lists
ArrayList LinkedList Vector 顺序添加 抽象数据类型(ADT)是一个实现包括储存数据元素的存储结构以及实现基本操作的算法. ArrayList (1)ArrayList是 ...
- linux 上挂载硬盘或者读取u盘数据
查看服务器上有哪些设备 df -hl查询挂载 硬盘后或者插上u盘后sda 的变化,新增的就是我们添加上的. fdisk -ls /dev/sda
- (数据科学学习手札59)从抓取数据到生成shp文件并展示
一.简介 shp格式的文件是地理信息领域最常见的文件格式之一,很好的结合了矢量数据与对应的标量数据,而在Python中我们可以使用pyshp来完成创建shp文件的过程,本文将从如何从高德地图获取矢量信 ...
- ArcGis 字段计算器表达式(Field calculator expression).cal文件与标注表达式(label expression).lxp的实质及其编码方式、解析方法
ArcGis 字段计算器表达式可以保存为一个扩展名为.cal的文件,该文件的实质是一个文本文件,编码方式为 UTF-16-LE. 官方的说明是“ArcGIS 应用程序使用 UTF-16-LE 编码读写 ...
- 前端自动化构建工具——gulp环境搭建教程
gulp是前端工程化的工具,它可以对html,css,js等代码和图片进行压缩,也可以对sass和less等预处理语言进行编译,代码部署.gulp学起来简单,用起来方便,大大提高我们工作效率. 这里可 ...
- UVA11427 Expect the Expected 概率dp+全概率公式
题目传送门 题意:小明每晚都玩游戏,每一盘赢的概率都是p,如果第一盘就赢了,那么就去睡觉,第二天继续玩:否则继续玩,玩到赢的比例大于p才去睡:如果一直玩了n盘还没完成,就再也不玩了:问他玩游戏天数的期 ...
- 27 string类中常用的方法列表
1. 获取方法 int length() 获取字符串的长度 char charAt(int index) 获取特定位置的字符 (角标越界) int indexOf(String str) 获取 ...
- MapReduce分区数据倾斜
什么是数据倾斜? 数据不可避免的出现离群值,并导致数据倾斜,数据倾斜会显著的拖慢MR的执行速度 常见数据倾斜有以下几类 1.数据频率倾斜 某一个区域的数据量要远远大于其他区域 2.数据大小倾斜 ...