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. " ...
随机推荐
- 卿烨科技 Fireball
9人开发病毒感染超2亿电脑!Fireball病毒境外做案被举报 原标题:名校毕业生研发病毒年获利8000万 2.5亿台电脑感染 海淀网友协助民警追踪跨境黑客 高材生开公司研发病毒 一年获利8000 ...
- npm install 超时 国内 切换源; npm ERR! code ELIFECYCLE;
install 超时 查看npm源地址 npm config get registry #http://registry.npmjs.org 为国外镜像地址 设置阿里云镜像 npm config se ...
- JAVA学习之Java程序开发初次体验
Java环境搭建算完成了,那么接下来写个Java程序走一个 开发Java程序的简单流程 1.将Java代码编写到扩展名为.java的文件中2.通过javac命令对该Java文件进行编译(生成class ...
- Awesome Adb——一份超全超详细的 ADB 用法大全
https://github.com/mzlogin/awesome-adb https://www.cnblogs.com/bravesnail/articles/5850335.html ...
- 3、Cookie与Session之间有哪些区别或者是优缺点?
Cookie与Session之间有哪些区别或者是优缺点? 知道了Cookie与Session,我们来做一些简单的总结: 1.Cookie可以存储在浏览器或者本地,session只能存在服务器 ...
- Linux源码与编译出的目标文件汇编代码的一致性问题
start_kernel是内核启动时比较重要的一个函数,然而我发现一个问题,我编译出来的目标文件中的汇编代码与C源码并不完全对应,这是怎么一回事呢? asmlinkage void __init st ...
- 数据整理A
- 第十八天:CSV、JSON、Excel、SQLite
一.CSV文件 1.读取 reader = csv.reader(打开的file对象), reader为可迭代对象 2.用namedtuple映射列名 with open('apple.csv') a ...
- eduCF#61 C. Painting the Fence /// DP 选取k段能覆盖的格数
题目大意: 给定n m 接下来给定m个在n范围内的段的左右端 l r 求选取m-2段 最多能覆盖多少格 #include <bits/stdc++.h> using namespace s ...
- android Intent和IntentFilter
android的应用程序包含三种重要的组件:Activity.Service.BroadcastReceiver,应用程序采用一致的方式来启动他们——都是依靠Intent来进行启动.Intent就封装 ...