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,23,2,1 → 1,2,31,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 ...
随机推荐
- Mybatis第一篇【介绍、快速入门、工作流程】
什么是MyBatis MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为 ...
- Oracle总结第一篇【基本SQL操作】
前言 在之前已经大概了解过Mysql数据库和学过相关的Oracle知识点,但是太久没用过Oracle了,就基本忘了-印象中就只有基本的SQL语句和相关一些概念-.写下本博文的原因就是记载着Oracle ...
- AJAX应用【股票案例】
股票案例 我们要做的是股票的案例,它能够无刷新地更新股票的数据.当鼠标移动到具体的股票中,它会显示具体的信息. 我们首先来看一下要做出来的效果: 服务器端分析 首先,从效果图我们可以看见很多股票基本信 ...
- MySQL_日期函数汇总
如果转载,请注明博文来源: www.cnblogs.com/xinysu/ ,版权归 博客园 苏家小萝卜 所有.望各位支持! 关于MySQL日期时间函数,每回总 ...
- 新建maven项目遇到Select an Archetype时没有maven-archetype-webapp处理方法
[已经有很多博客写过相关的了.详细请去看其他博主的.这里只是记录新建的时候发生的问题给新手提供帮助.因为我跟我的同事都遇到了.因为没记录下来,又花了时间找问题.而网上好像也不多.所以记录下来.希望帮到 ...
- jmeter3.3测试需要登录的接口(java)
1.新建线程组-略过 2.右键线程组->添加->配置元件->HTTP授权管理器 3.右键线程组->添加->配置元件->HTTP信息头管理器 4.右键线程组-> ...
- ThinkPHP中:RBAC权限控制的实习步骤
使用版本ThinkPHP3.1.3 第一步,建表及数据 第二步,建关联模型 第三步,控制器使用关联模型.配置文件 第四步,模板显示数据 第一步,建表及数据 在数据库中,建立一个companysvn数据 ...
- Nginx学习——Nginx基本配置
1.Nginx的配置文件总览 Nginx配置文件详解 : http://www.cnblogs.com/hunttown/p/5759959.html nginx.conf 基本格式: worker_ ...
- hdu3756三分基础题
Dome of Circus Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- PyCharm基本操作
1.1 PyCharm基本使用 视频学习连接地址:http://edu.51cto.com/course/9043.html 1.1.1 在Pycharm下为你的Python项目配置Python解释器 ...