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. qtp识别验证码

    花了两天时间才完整的完成识别验证码的登录操作,在网上看到很多关于验证码识别的方法,但是我用的qtp版本比较高级,所以还是要自己花心思研究.po上我的识别验证码的详细历程: 一.读取浏览器中的图片验证码 ...

  2. Monkeyrunner自动化测试由浅入深(第一节)

    (原版)Monkeyrunner自动化测试由浅入深(第一节) 博主原创,请勿转载 第一.相关软件和环境的配置 1.Android sdk下载和配置 2.java jdk下载和配置 第二.Monkeyr ...

  3. python-opencv学习第一章

    一.opencv读入图像 retval=cv2.imread(文件名[,显示控制参数]) 常用三个参数如下 CV. IMREAD_UNCHANGED 不改变原先什么样子现在就是什么样子 CV. IMR ...

  4. linux cut sort wc sed>vi awk (文本处理)

    cut: 显示切割的行数据 -f: 选择显示的列 (1: 显示第一列; 1,3: 显示第一列.第三列; 1-3: 显示第一列到第三列) -s: 不显示没有分隔符的行 -d: 自定义分隔符(' '空格 ...

  5. 2018-2019-2 网络对抗技术 20165220 Exp 9 Web安全基础

    2018-2019-2 网络对抗技术 20165220 Exp 9 Web安全基础 实验任务 本实践的目标理解常用网络攻击技术的基本原理,做不少于7个题目,共3.5分.包括(SQL,XSS,CSRF) ...

  6. nginx中lua动态返回文件

    原来还可以这么操作,lua动态获取内容然后返回,下面是实例,可以做到先返回一个字符串,然后过5秒再返回另外一个字符串 ngx.say("hello") ngx.flush(true ...

  7. P1076 寻宝

    P1076 寻宝 题解 这道题真是感人啊,废了蒟蒻一天的时间 关键 1. a[ k ][ ] 数组记录第k层有楼梯房间的编号 a[ k ][ 0 ]  第k层有几个有楼梯的房间 a[ k ][ i ] ...

  8. docker-容器完整构建过程

    container 代码app,构建,运行,分享(推送)image mkdir img1 cd img1 [root@cu-tmp-201 img1]# ls app.py Dockerfile re ...

  9. 安装ssh

    1.win10 安装ssh sudo apt-get remove --purge openssh-server ## 先删ssh sudo apt-get install openssh-serve ...

  10. 【转载】inno setup 水波纹效果,检测安装vcredist_x86.exe等

    以下inno setup脚本,实现了:1.水波纹效果 2.安装时检测是否安装其他版本,并在欢迎页面添加文字提示 4.检测安装vcredist_x86.exe  3.卸载时添加提示 ; 脚本由 Inno ...