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 ...
随机推荐
- wpf datagrid 行双击事件
Xaml: <DataGrid ItemsSource="{Binding SessionList}" Grid.Row="2" Grid.Column= ...
- linux标准输入输出重定向
command > filename 把标准输出重定向到一个文件,如果文件不存在则新建,如果存在则覆盖其内容.command >> filename 把标准输出重定向到一个文件中,如 ...
- 查看某一个点是否在某个多边形内 使用ST_Contains函数
查看某一个点是否在某个多边形内 使用ST_Contains函数 --LINESTRING ( 121.312350 30.971457 , 121.156783 31.092221 , 121.35 ...
- mysql 连接多行 合并多行
group_concat() select group_concat(id) from xxxx -------------------------------------------- id1,id ...
- PD 脚本中列名注释用Name属性
操作步骤:Database=>Generate Datatabase=>Format选项卡=>勾选 Generate name in empty comment项
- 解决 Eclipse build workspace 慢,validation javascript 更慢的问题
鸣谢:http://zuoming.iteye.com/blog/1430925 ------------------------------------------------ 如果用到js插件或者 ...
- 【递推】BZOJ 3930: [CQOI2015]选数
Description 我们知道,从区间[L,H](L和H为整数)中选取N个整数,总共有(H-L+1)^N种方案.小z很好奇这样选出的数的最大公约数的规律,他决定对每种方案选出的N个整数都求一次最大公 ...
- 团体程序设计天梯赛-练习集L1-001. Hello World
L1-001. Hello World 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 这道超级简单的题目没有任何输入. 你只需要在一行中输 ...
- Windows下的Memcache安装与测试教程
Windows下的Memcache安装 1.下载memcache for windows. 下载地址:http://splinedancer.com/memcached-win32/,推荐下载bina ...
- POJ1002487-3279(map)
http://poj.org/problem?id=1002 题意:是说很多公司用了容易记住的电话号码,例如有英文字母的或者是用了很多连字符或没有连字符的.每个电话号码都有标准模式,而为了统计有没有重 ...