leetcode 31. Next Permutation JAVA
题目:
实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列。
如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列)。
必须原地修改,只允许使用额外常数空间。
以下是一些例子,输入位于左侧列,其相应输出位于右侧列。1,2,3 → 1,3,23,2,1 → 1,2,31,1,5 → 1,5,1
【0】【1】【2】
解题思路:
题目的总体思路是。从后往前读。当后面的数比前面的数大时,再开一个循环,从后开始于当前数比較。当比当前数大时,交换。然后再从当前数的后一位開始,直到最后反序就可以。
class Solution {
public void nextPermutation(int[] nums) { //高位为nums[0]
if(nums != null && nums.length >1){
int i;
for(i = nums.length-2;i>=0;i--){
if(nums[i+1]>nums[i]){
break;
}
}
if(i >= 0){ //如果整个序列为逆序时,i小于0 reverse整个序列,否则找到比nums[i]大的交换次序
int k;
for(k=nums.length-1;k>=0;k--){
if(nums[k]>nums[i]){
break;
}
}
swap(nums,k,i);
}
reverse(nums, i+1);
}
}
private void reverse(int[] nums, int start) {
int left = start;
int right = nums.length - 1;
while (left < right) {
swap(nums, left, right);
left++;
right--;
}
}
private void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
leetcode 31. Next Permutation JAVA的更多相关文章
- [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 ...
- Java [leetcode 31]Next Permutation
题目描述: Implement next permutation, which rearranges numbers into the lexicographically next greater p ...
- 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 ...
随机推荐
- SpringBoot 项目打war包 tomcat部署
今天看了一下springboot的项目,个人习惯是接触新的语言或框架,首先要做的就是程序员届最常用的“Hello World”,然后进行项目部署,然今天部署却发现一直都是404,查看tomcat的we ...
- C# Equals
[C# Equals] 1.Object.Equals() The type of comparison between the current instance and the obj parame ...
- C# Common Keyword II
[C# Common Keyword II] 1.as 运算符用于在兼容的引用类型之间执行某些类型的转换. class csrefKeywordsOperators { class Base { pu ...
- 原生JS的使用,包括jquery和原生JS获取节点、jquery和原生JS修改属性的比较
一.前言 相比于JS这条直达终点.满是荆棘的小路,jquery无疑是康庄大道了,足够的简洁.易用给了它辉煌的地位.然而,毕竟是绕着道的插件,当小路走着走着变成大路的时候,曾经的大路也就失去了他自身的优 ...
- 256. Paint House房屋染色
[抄题]: There are a row of n houses, each house can be painted with one of the three colors: red, blue ...
- qmake not exec in ubuntu
Description Today, I want to run qmake command in terminal, but, it has error message such qmake: co ...
- ubuntu 14.04 Clion2016.2 安装激活与安装后添加快捷启动方式
参考链接:http://www.cnblogs.com/conw/p/5938113.html 下载clion for linux : http://www.jetbrains.com/clion/d ...
- 解决table边框在打印中不显示的问题
先了解一下,table边框如何设置 一.只对表格table标签设置边框 只对table标签设置border(边框)样式,将让此表格最外层table一个边框,而表格内部不产生边框样式.CSS代码: .t ...
- Spring boot 开发组件
一.Jboot 描述:Jboot是一个基于jfinal 和 undertow开发的微服务框架.提供了AOP.RPC.分布式缓存.限流.降级.熔断.统一配置中心.swagger api自动生成.Open ...
- 文件上传控件asp:FileUpload
前端 使用的控件<asp:FileUpload ID="fileup" runat="server" /><span class=" ...