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
题目标签:Array
Java Solution:
Runtime beats 85.79%
完成日期:07/13/2017
关键词:Array
关键点:找到需要被增大的那个数字A,再找到最接近于A且比A大的数字B,互换A和B,最后把B之后的所有数字sort
public class Solution
{
public void nextPermutation(int[] nums)
{
// from right to left, find the first number which is not in ascending order
for(int i=nums.length-1; i>=0; i--)
{
if(i == 0) // meaning all number are in ascending order
{
Arrays.sort(nums);
return;
}
else if(nums[i] > nums[i-1])
{
// from right to left, find the first number is greater than nums[index_num]
for(int j=nums.length-1; j>=i-1; j--)
{
if(nums[j] > nums[i-1]) // swap them
{
int temp = nums[i-1];
nums[i-1] = nums[j];
nums[j] = temp;
// sort the rest numbers after i - 1
Arrays.sort(nums, i, nums.length);;
return;
}
}
}
}
} }
参考资料:
http://www.cnblogs.com/grandyang/p/4428207.html
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 31. Next Permutation (下一个排列)的更多相关文章
- [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] Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 【LeetCode每天一题】Next Permutation(下一个排列)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- LeetCode(31): 下一个排列
Medium! 题目描述: (请仔细读题) 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列) ...
- Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 031 Next Permutation 下一个排列
实现获取下一个排列函数,这个算法需要将数字重新排列成字典序中数字更大的排列.如果不存在更大的排列,则重新将数字排列成最小的排列(即升序排列).修改必须是原地的,不开辟额外的内存空间.这是一些例子,输入 ...
- lintcode:next permutation下一个排列
题目 下一个排列 给定一个整数数组来表示排列,找出其之后的一个排列. 样例 给出排列[1,3,2,3],其下一个排列是[1,3,3,2] 给出排列[4,3,2,1],其下一个排列是[1,2,3,4] ...
- 31. Next Permutation (下一个全排列)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
随机推荐
- Fabric 1.0的多机部署
Fabric1.0已经正式发布一段时间了,官方给出的单机部署的脚本也很完备,基本上傻瓜式的一键部署,直接运行官方的network_setup.sh up即可.但是在实际生产环境,我们不可能把所有的节点 ...
- 安装wampserve之前需要安装vc++2012.
本人是64位系统下载了wampserver3.0.6之后安装好,启动报错缺少msvcr110.dll. 于是从网上下载了msvcr110.dll放到了windows的syswow64文件夹下,甚至还重 ...
- 私人订制——属于你自己的Linux
一.前言 Linux操作系统至1991年10月5日诞生以来,就其开源性和自由性得到了很多技术大牛的青睐,每个Linux爱好者都为其贡献了自己的一份力,不管是在Linux内核还是开源软件等方面,都为我们 ...
- Python-老男孩-02_装饰器_面向对象_封装_继承_异常_接口_数据库
装饰器其实也是一个函数,它的参数是一个函数 ; 其它函数与装饰器之间建立联系是通过 @装饰器函数名, 感觉有点像Spring的面向切面编程 装饰器函数,如何处理原函数的参数.? 装饰器 原函数返回值 ...
- Activiti-01
1, Activiti官网:http://www.activiti.org/ 主页可以看到jar包的下载. 2, 进入http://www.activiti.org/userguide/index. ...
- python 实现注册程序
本文介绍用python实现一个模拟注册的程序,详细需求如下: # 写一个注册的程序,输入username,密码,密码确认,输入的账号和密码不能为空,两次输入密码必须一致,用户名不能重复,错误次数4次# ...
- D - DZY Loves Hash CodeForces - 447A
DZY has a hash table with p buckets, numbered from 0 to p - 1. He wants to insert n numbers, in the ...
- Android Studio 导入应用时报错 Error:java.lang.RuntimeException: Some file crunching failed, see logs for details
在app文件夹的build.gradle里加上 android { ...... aaptOptions.cruncherEnabled = false aaptOptions.useNewCrunc ...
- 表空间tablespace,数据文件datafiles,和控制文件control files介绍
https://docs.oracle.com/cd/B19306_01/server.102/b14220/physical.htm#i5919 本文系翻译 表空间tablespace,数据文件da ...
- BZOJ-5055-膜法师(离散化+树状数组)
Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然,他能为长者所续的时间,为这三个维度上能量的乘 ...