Java [leetcode 31]Next Permutation
题目描述:
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, do not allocate 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
解题思路:
从整个数组的最后开始往前判断,直到找到相邻两个数是增加的,记录下这个位置为i;
如果i小于0,表示整个数组的顺序是递减的;
从数组的最后开始寻找,找到第一个大于nums[i]的数的位置,记为j;
交换nums[i]和nums[j]的位置,然后对i后的所有数组进行倒序排列。
注意边界问题,数组不要越界。
代码如下:
public class Solution {
public void nextPermutation(int[] nums) {
if (nums.length < 2)
return;
int i, j, temp;
for (i = nums.length - 2; i >= 0; i--) {
if (nums[i] < nums[i + 1])
break;
}
for (j = nums.length - 1; j >= 0; j--) {
if (i < 0)
break;
if (nums[j] > nums[i])
break;
}
if (i >= 0) {
temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
if (i < nums.length - 2) {
for (int k = i + 1; k <= (nums.length - i - 2) / 2 + i + 1; k++) {
temp = nums[k];
nums[k] = nums[nums.length + i - k];
nums[nums.length + i - k] = temp;
}
}
}
}
Java [leetcode 31]Next Permutation的更多相关文章
- [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 JAVA
题目: 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改,只允许使用额外常数 ...
- LeetCode 31. Next Permutation (下一个排列)
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 per ...
随机推荐
- 在win7上建立本地FTP站点详细步骤
一.安装FTP组件点击:控制面板—>程序和功能—>打开或关闭Windows功能. 勾选“FTP服务器”及“FTP服务”“FTP扩展性”,点击“确定”,安装FTP组件. 勾选Web管理工具的 ...
- 几种解析xml方式的比较
1: DOM DOM 是用与平台和语言无关的方式表示 XML 文档的官方 W3C 标准.DOM 是以层次结构组织的节点或信息片断的集合.这个层次结构允许开发人员在树中寻找特定信息.分析该结构通常需要加 ...
- 转:深入学习Oracle分区表及分区索引
转自:http://database.ctocio.com.cn/tips/286/8104286.shtml 关于分区表和分区索引(About Partitioned Tables and Inde ...
- 解决ora-01652无法通过128(在表空间temp中)扩展temp段
问题描述: 今天建索引的时候报:ora-01652无法通过128(在表空间temp中)扩展temp段 1.查看表空间是自动增长,且建表空间时是没有设表空间最大值的. 2.查看了一下表空间剩余多少竟然只 ...
- 移动端webapp开发必备知识
移动设备的用户越来越多,每天android手机的激活量都已经超过130万台,所以我们面向移动终端的WebAPP也开始跟进了.本文主要介绍webapp的开发与调试的相关知识和经验,以及给出几种可选的解决 ...
- hadoop分布式安装教程(转)
from:http://www.cnblogs.com/xia520pi/archive/2012/05/16/2503949.html 1.集群部署介绍 1.1 Hadoop简介 Hadoop是Ap ...
- selenium + python网页自动化测试环境搭建
1.python的安装 ,这个不解释,exe文件运行安装即可,既然你选择python,相信你是熟悉python的,我安装目录C:\Python27 2.setuptools 的安装也非常简单,同样是e ...
- 原生JS的对象常用操作总结
前端时间写了篇怎么判断js对象相等的文章,一直在期待大神给点消息,无奈一直杳无音讯,还是自己写个函数来进行判断,下面总结一些常用的对象操作的方法. 咋们来个先抑后扬的方式,先放出几个基本的 ...
- ASP.NET遍历textbox文本框
Asp.Net如何遍历所有TextBox控件并清空 asp.net 不能像window那样直接遍历this.Controls就可以了,因为: this.Controls只是包含了Page根一级的con ...
- CSRF注入式攻击防御讲解
0x01 什么是CSRF攻击 CSRF是Cross Site Request Forgery的缩写(也缩写为XSRF),直译过来就是跨站请求伪造的意思,也就是在用户会话下对某个CGI做一些GET/PO ...