LeetCode OJ 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
【题目分析】
题目要求我们对一个数组进行变换,变换后的结果是按照字典序比当前结果大的值中最小的那一个。如果我们不能找到下一个permutation(即当前顺序是这些数字最大的字典序排列),那么直接给出一个逆序的结果。
【思路】
1,2,3 → 1,3,23,2,1 → 1,2,31,1,5 → 1,5,1
分析这几个例子我们发现,如果一段序列是完全降序排列的,那么它的字典序是最大的,即对于这个序列中任意相邻的两个数a和b,a>=b。如果存在这样相邻的两个数a < b,那么通过变换我们可以找到它的下一个permutation。比如1,2,3 -> 1,3,2 我们交换2和3即可得到下一个结果。但是考虑这样一个序列: 4,6,5,我们看到4比6小,如果我们交换3和6得到的结果是6,4,5,这是我们想要的结果吗?并不是,我们发现,虽然相邻的4和6相邻且6比4大,但是在6的右边,还存在一个5它比4大,但是它比6小,如果我们交换5和4,得到的结果才是正确的。
因此这个过程可以这样来描述:
1. 找到第一个相邻的元组(a,b),满足a < b;
2. 如果这样的元组不存在,则将数组逆序,程序结束;
3. 如果这样的元组存在,我们找到从b开始数组以后的数中比a大的最小的那一个c。
4. 交换a和c,然后把c之后所有的数逆序,得到最后的结果,返回;
例如:[4,6,5,3,2,1]
第一步:找到元组(4,6) 4 < 6
第二步:找到6之后比4大的元素中最小的那个:5
第三步:交换4和5->[5,6,4,3,2,1,1]
第四步:把5之后的数字逆序->[5,1,1,2,3,4,6]
通过上述几个步骤,我们能正确地找出任意一个序列的next greater permutation
【java代码】
public class Solution {
public void nextPermutation(int[] nums) {
int len = nums.length;
if(len < 2) return;
//找到第一个降序的数
int index;
for(index = len - 2; index >= 0; index--){
if(nums[index] < nums[index+1]) break;
}
//如果没有这样的数则直接返回逆序的结果
if(index < 0){
for(int i = 0,j = len - 1;i < j; i++, j--){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
return;
}
//找到降序的数的右边比它大的数中最小的那一个
int greater = index + 1;
while(greater < len - 1 && nums[greater + 1] > nums[index]){
greater ++;
}
//第一个降序的数和它右边比他大的最小的那个数交换
int temp = nums[index];
nums[index] = nums[greater];
nums[greater] = temp;
//第一个降序数位置之后所有数逆序,得到最后的结果
for(int i = index + 1, j = len - 1; i < j; i++, j--){
temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}
LeetCode OJ 31. Next Permutation的更多相关文章
- <LeetCode OJ> 31. Next Permutation
31. Next Permutation Total Accepted: 54346 Total Submissions: 212155 Difficulty: Medium Implement ne ...
- leetcode个人题解——#31 Next Permutation
写这题时脑子比较混乱,重写了一遍wiki大佬的解法. 算法: According to Wikipedia, a man named Narayana Pandita presented the fo ...
- [Leetcode][Python]31: Next Permutation
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 31: Next Permutationhttps://oj.leetcode ...
- [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 ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
随机推荐
- ascii 转换为 utf-8
Python在安装时,默认的编码是ascii,当程序中出现非ascii编码时,python的处理常常会报这样的错: UnicodeDecodeError: 'ascii' codec can't de ...
- Unity3d请求webservice
我们在对接第三方sdk时,第三方sdk通常会以一个webservice接口的形式供我们来调用.而这些接口会以提供我们get,post,soap等协议来进行访问.get,post方法相信大家都比较熟悉了 ...
- Java 彩色图转灰度图
1. 方法1 BufferedImage grayImage = new BufferedImage(width, height, colorImage.TYPE_BYTE_GRAY); Graphi ...
- 《HTML5权威指南》
<HTML5权威指南> HTML元素: html字符实体 html全局属性 html base标签 用元数据元素说明文档 标记文字(第八章) 标记文字.组织内容.文档分节 表格元素 表单元 ...
- UNIX基础--Manual Pages
联机手册 Manual Pages 最详细的使用说明文档莫过于 FreeBSD 里的联机手册了. 几乎每一个程序都会附上一份简短说明, 以介绍这个程序的的基本功能以及参数的用法. 我们能通过 man ...
- 利用python 与 wmi 获取WINDOWS基本信息
#!/usr/bin/env python3.5 # -*- coding:utf8 -*- import platform import subprocess import wmi def serv ...
- 用微软makecert.exe生成一个自签名的证书
RT makecert.exe不用去找,安装VS2008后,在开始菜单相应的路径找到该命令提示符:Microsoft Visual Studio 2008/Visual Studio Tools/Vi ...
- C#模板打印功能-模板为WPS或Excel
//---WPS----- using EtApp = ET; using System.Reflection; using System.Runtime.InteropServices; using ...
- EBS FORM FOLDER 开发,单元格无法使用右键
问题描述: 在使用folder开发FORM后,单元格无法使用右键,正常应该可以右键进行隐藏.显示.复制等操作. 通过对比发现是因ITEM属性中 弹出式菜单未设置导致. 解决方法: 设置弹出式菜单
- 30分钟掌握ES6/ES2015核心内容
30分钟掌握ES6/ES2015核心内容 ECMAScript 6(以下简称ES6)是JavaScript语言的下一代标准.因为当前版本的ES6是在2015年发布的,所以又称ECMAScript ...