题目:

实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列。

如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列)。

必须原地修改,只允许使用额外常数空间。

以下是一些例子,输入位于左侧列,其相应输出位于右侧列。
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,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的更多相关文章

  1. [array] leetcode - 31. Next Permutation - Medium

    leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...

  2. LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]

    LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...

  3. LeetCode - 31. Next Permutation

    31. Next Permutation Problem's Link ---------------------------------------------------------------- ...

  4. leetcode 31. Next Permutation (下一个排列,模拟,二分查找)

    题目链接 31. Next Permutation 题意 给定一段排列,输出其升序相邻的下一段排列.比如[1,3,2]的下一段排列为[2,1,3]. 注意排列呈环形,即[3,2,1]的下一段排列为[1 ...

  5. Java [leetcode 31]Next Permutation

    题目描述: Implement next permutation, which rearranges numbers into the lexicographically next greater p ...

  6. LeetCode 31. Next Permutation (下一个排列)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  7. [LeetCode] 31. Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  8. leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法

    Next Permutation  Implement next permutation, which rearranges numbers into the lexicographically ne ...

  9. leetcode 31. Next Permutation(字典序的下一个)

    描述: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...

随机推荐

  1. struts2配置文件(struts.xml)中相关属性的设置

    <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-/ ...

  2. MySQL数据库篇之存储引擎

    主要内容: 一.数据引擎 二.MySQL支持的存储引擎 三.使用存储引擎 1️⃣ 什么是存储引擎? MySQL中建立的库----> 文件夹,库中建立的表----->文件. 现实生活中我们用 ...

  3. java 修改文件

    public void fileAlter(String fileName,String content) throws IOException{                 BufferedRe ...

  4. curl 访问 k8s

    curl https://mhc:6443/api --cacert ssl/ca.crt --key client_ssl/cs_client.key --cert client_ssl/cs_cl ...

  5. 在SharePoint解决方案中使用JavaScript (2) – 模块化

    本文是在SharePoint中使用JavaScript的第二篇文章,前面的文章包括: 在SharePoint解决方案中使用JavaScript (0) 在SharePoint解决方案中使用JavaSc ...

  6. 10 华电内部文档搜索系统 search02

    搜索项目并不是一个很大的项目,在实际项目中往往是作为子项目和别的项目集成在一起的.比如说和OA项目集成在一起,作为另外一个项目的子系统来使用.搜索项目的功能并不复杂. 整个项目是文档搜索项目,如题:企 ...

  7. cdoj915-方老师的分身 II (长度不小于k的最短路)【spfa】

    http://acm.uestc.edu.cn/#/problem/show/915 方老师的分身 II Time Limit: 10000/5000MS (Java/Others)     Memo ...

  8. 解剖Nginx·模块开发篇(4)模块开发中的命名规则和模块加载与运行流程

    1 命名规则 1.1 基本变量 基本变量有三个: ngx_module_t 类型的 ngx_http_foo_bar_module: ngx_command_t 类型的数组 ngx_http_foo_ ...

  9. 【Unity】Collider随骨骼动画运动

    Collider位置和角色的动作不一致会导致Mesh互相镶嵌,让游戏失真. 想象一扇门的Collider没随它打开的动画移动,结果就是你看着门开着却穿不过去. 而我遇到的情况是:     角色在执行跑 ...

  10. ”$-”与shell默认选项

    一.前言 之所以整理这篇博客,主要是写Linux环境设置文件 的时候,在查看/etc/profile时看到这么一段代码: for i in /etc/profile.d/*.sh ; do if [ ...