LeetCode: 31. Next Permutation (Medium)
1. 原题链接
https://leetcode.com/problems/next-permutation/description/
2. 题目要求

给出一个整型数组,让我们给出下一个排序情况。注意以下规则:
(1)下一个排列必须比原排列要大。例如“1,2,4,5,3”,下一个排列为“1,3,4,5,2”,比之前的排列要大;
(2)如果给出的数组已经按降序排列,则下一个排列必须是升序排列。例如“5,4,3,2,1”为降序,下一个排列必须为升序“1,2,3,4,5”;
全排列概念:
3. 解题思路
从后向前找到,前一个元素 nums[ i ] 小于后一个元素 nums[ i+1 ] 的位置。然后在从后向前找到 刚好大于nums[ i ] 的元素 nums [ j ],交换 nums[ i ] 和 nums[ j ] 。最后将nums[ i ] 之后的元素进行逆置。
4. 代码实现
public class NextPermutation31 {
public static void main(String[] args) {
int[] nums ={1,2,4};
NextPermutation31 np =new NextPermutation31();
for(int x:np.nextPermutation(nums))
System.out.println(x);
}
public int[] 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,nums.length-1);
return nums;
}
public void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
public void reverse(int[] nums, int start, int end) {
while (start < end) {
swap(nums, start, end);
start++;
end--;
}
}
}
LeetCode: 31. Next Permutation (Medium)的更多相关文章
- [array] leetcode - 31. Next Permutation - Medium
leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...
- LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]
LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...
- LeetCode - 31. Next Permutation
31. Next Permutation Problem's Link ---------------------------------------------------------------- ...
- leetcode 31. Next Permutation (下一个排列,模拟,二分查找)
题目链接 31. Next Permutation 题意 给定一段排列,输出其升序相邻的下一段排列.比如[1,3,2]的下一段排列为[2,1,3]. 注意排列呈环形,即[3,2,1]的下一段排列为[1 ...
- LeetCode 31. Next Permutation【Medium】
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- [LeetCode] 31. Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法
Next Permutation Implement next permutation, which rearranges numbers into the lexicographically ne ...
- LeetCode 31. Next Permutation (下一个排列)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- Java [leetcode 31]Next Permutation
题目描述: Implement next permutation, which rearranges numbers into the lexicographically next greater p ...
随机推荐
- 使用jmeter使用Jenkins发送自定义消息内容
Jenkins运行成功后,需要发送消息给用户,自己封装了一个rtx的方法,进行发送,配置方法如下: 1.在windows下选择 execute windows batch command,执行我的py ...
- Ajax向Controller发送请求并接受数据需要注意的一个细节
想用Ajax想向Controller发送请求和接收返回的字符等等.Controller中要使用@ResponseBody注解. <script type="text/javascrip ...
- 2017 计蒜之道 初赛 第一场 A、B题
A题 阿里的新游戏 题目概述: 阿里九游开放平台近日上架了一款新的益智类游戏——成三棋.成三棋是我国非常古老的一个双人棋类游戏,其棋盘如下图所示: 成三棋的棋盘上有很多条线段,只能在线段交叉点上放入棋 ...
- programming-languages学习笔记--第2部分
programming-languages学习笔记–第2部分 */--> pre.src {background-color: #292b2e; color: #b2b2b2;} pre.src ...
- 快速理解Event事件
浏览器事件是所有web程序的核心.javascript与HTML之间的交互是通过事件实现的.通过这些事件我们定义将要发生的行为.事件是一种异步编程的实现方式,本质上是程序各个组成部分之间的通信. 1. ...
- es6之数组方法
//兼容插件 babel-polyfill values()等存在兼容问题,需要加载babel-polyfill插件 .keys() 获取数组的key值 .values() 获取数组的value值 ...
- Css3 实现关键帧动画
<div class="person"> </div> <script> var str1 = "@keyframes move{&q ...
- VC++和C语言中常见数据类型转换为字符串的方法
1.短整型(int) itoa(i,temp,10);///将i转换为字符串放入temp中,最后一个数字表示十进制 itoa(i,temp,2); ///按二进制方式转换 2.长整型(long) lt ...
- 简要的谈一谈我对CSS中长度单位的理解
CSS中的长度单位目前分为两种,分别是绝对长度和相对长度.绝对长度单位包括: in:英寸 cm:厘米 mm:毫米 pt:磅(1磅等于1/72英寸) pc:pica(1pica等于12磅) 以上五个就是 ...
- 过滤xss攻击脚本
<?php /** * @blog http://www.phpddt.com * @param $string * @param $low 安全别级低 */ function clean_xs ...