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 and use only constant 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

 
class Solution {
public void nextPermutation(int[] nums) {
int tmp;
int i;
int j;
for(i = nums.length-2; i >= 0; i--){
if(nums[i] < nums[i+1]) break;
} if(i >= 0){
//find the smallest num in the right which is larger than num[i]
for(j = nums.length-1; j >= i; j--){
if(nums[j] > nums[i]) break;
} //swap these two num
tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp; //sort
Arrays.sort(nums, i+1, nums.length);
}
else{
Arrays.sort(nums);
}
} }

寻找规律:

- 从右向左扫描,找到小于右侧数字的第一个数字

- 将右侧大于它的最小的那个数放在该位,它和其余右侧的数字从小到大排列放在右侧。

31. Next Permutation (JAVA)的更多相关文章

  1. 31. Next Permutation (java 字典序生成下一个排列)

    题目: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...

  2. leetcode 31. Next Permutation JAVA

    题目: 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改,只允许使用额外常数 ...

  3. LeetCode - 31. Next Permutation

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

  4. [Leetcode][Python]31: Next Permutation

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 31: Next Permutationhttps://oj.leetcode ...

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

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

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

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

  7. &lt;LeetCode OJ&gt; 31. Next Permutation

    31. Next Permutation Total Accepted: 54346 Total Submissions: 212155 Difficulty: Medium Implement ne ...

  8. 刷题31. Next Permutation

    一.题目说明 题目是31. Next Permutation,英文太差看不懂,翻译了一下.才知道是求字典顺序下的下一个排列,不允许使用额外空间.题目难度是Medium! 二.我的实现 首先要进一步理解 ...

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

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

随机推荐

  1. Centos7系统备份与恢复教程

    原文地址:https://www.cnblogs.com/fang888/p/8473485.html Centos系统备份与恢复教程 tar: 特点 1.保留权限 2.适合备份整个目录 3.可以选择 ...

  2. 详解设备PID和VID

    根据USB规范的规定,所有的USB设备都有供应商ID(VID)和产品识别码(PID),主机通过不同的VID和PID来区别不同的设备. VID和PID都是两个字节长,其中,供应商ID(VID)由供应商向 ...

  3. HDU6534 Chika and Friendly Pairs(莫队,树状数组)

    HDU6534 Chika and Friendly Pairs 莫队,树状数组的简单题 #include<bits/stdc++.h> using namespace std; cons ...

  4. C++中cout输出字符串和字符串型指针地址值的方法以及C语言中的printf用法比较

    #include <iostream> using namespace std; #include <stdio.h> int main() { char *pstr = &q ...

  5. java中FastJson的json类型转换

    JSON Gson: 来自Google,功能全面.快速.简洁.面向对象.数据传递和解析方便. Jackson:来源FasterXML项目,社区活跃,更新快 解析速度和效率比Gson快,但无法按需解析, ...

  6. httpd启动显示Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName'

    AH00557: httpd: apr_sockaddr_info_get() failed for masterAH00558: httpd: Could not reliably determin ...

  7. 阶段3 1.Mybatis_07.Mybatis的连接池及事务_3 mybatis连接池的分类

    2.mybatis中的连接池     mybatis连接池提供了3种方式的配置:         配置的位置:             主配置文件SqlMapConfig.xml中的dataSourc ...

  8. vue 请求完接口后执行方法

    getLunbo: function() { var that = this; that.lunbo = []; // api.showProgress({ // title: '加载中' // }) ...

  9. RabbitMQ使用(下)

    RabbitMQ从信息接收者角度可以看做三种模式,一对一,一对多(此一对多并不是发布订阅,而是每条信息只有一个接收者)和发布订阅.其中一对一是简单队列模式,一对多是Worker模式,而发布订阅包括发布 ...

  10. mybatisProxy

    config.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configurati ...