原题链接在这里:https://leetcode.com/problems/next-greater-element-iii/description/

题目:

Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly the same digits existing in the integer nand is greater in value than n. If no such positive 32-bit integer exists, you need to return -1.

Example 1:

Input: 12
Output: 21

Example 2:

Input: 21
Output: -1

题解:

如果每一位digit依次递减, 比如"54321"就没有next greater element. return -1.

所以说节点在于不是递减的位置, "52431", 找到 “2” 和 “4”的位置不是递减. 然后在2的后面比2大的最小digit, 这里是3.

"2", "3"换位, 变成"53421", 再把3后面的部分sort成从小到大 "53124"就是next greater element.

Time Complexity: O(x), x是n的digit 数目. 每个digit不会走超过3遍.

Space: O(x).

AC Java:

 class Solution {
public int nextGreaterElement(int n) {
char [] arr = (""+n).toCharArray();
int i = arr.length-2;
while(i>=0 && arr[i]>=arr[i+1]){
i--;
} if(i < 0){
return -1;
} int j = arr.length-1;
while(j>i && arr[j]<=arr[i]){
j--;
} swap(arr, i, j);
reverse(arr, i+1);
try{
return Integer.valueOf(new String(arr));
}catch(Exception e){
return -1;
}
} private void swap(char [] arr, int i, int j){
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
} private void reverse(char [] arr, int start){
int i = start;
int j = arr.length-1;
while(i < j){
swap(arr, i++, j--);
}
}
}

类似Next PermutationNext Greater Element INext Greater Element II.

LeetCode Next Greater Element III的更多相关文章

  1. [LeetCode] Next Greater Element III 下一个较大的元素之三

    Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly th ...

  2. LeetCode 556. 下一个更大元素 III(Next Greater Element III)

    556. 下一个更大元素 III 556. Next Greater Element III 题目描述 给定一个 32 位正整数 n,你需要找到最小的 32 位整数,其与 n 中存在的位数完全相同,并 ...

  3. 【LeetCode】556. Next Greater Element III 解题报告(Python)

    [LeetCode]556. Next Greater Element III 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

  4. [LeetCode] Next Greater Element II 下一个较大的元素之二

    Given a circular array (the next element of the last element is the first element of the array), pri ...

  5. [LeetCode] Next Greater Element I 下一个较大的元素之一

    You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...

  6. LeetCode——Next Greater Element I

    LeetCode--Next Greater Element I Question You are given two arrays (without duplicates) nums1 and nu ...

  7. [LeetCode] 556. Next Greater Element III 下一个较大的元素 III

    Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly th ...

  8. [Swift]LeetCode556. 下一个更大元素 III | Next Greater Element III

    Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly th ...

  9. 556. Next Greater Element III下一个更大的数字

    [抄题]: Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exac ...

随机推荐

  1. const修饰的常量 不能被直接修改 但是可以通过指针进行间接修改

    大家都知道如下代码中,被const限定的a是不可以被直接修改的 void main() { const int a = 3; a=1; } 在C++中const修饰的常量,不能被直接修改,但是可以通过 ...

  2. curator的版本兼容问题(需注意)

    Curator 存在版本兼容问题. Curator 2.x.x-兼容两个zk 3.4.x 和zk 3.5.x, Curator 3.x.x-兼容兼容zk 3.5. Versions The are c ...

  3. 为JAXB和response设置编码,解决wechat4j中文乱码

    如果有哪一个做程序员的小伙伴说自己没有遇到中文乱码问题,我是不愿意相信的.今天在做微信订阅号的智能回复时,又一时迷乱的跳进了中文乱码这个火坑.刚解决问题时,都欢呼雀跃了,完全忘记了她曾经带给我的痛苦. ...

  4. ES使用中遇到的多种坑,以及解决方案

    1.查询不到导致404报错 在使用get或者search进行查询获取文档的时候,如果没有结果会抛出404的异常. 我们当然不希望抛出异常,这时候就要使用ignore这个参数来忽略报错,ignore可以 ...

  5. 如何在深层嵌套ngRepeat中获取不同层级的$index

    <ul class="list-group" ng-repeat="item in vm.appData" ng-init="outerInde ...

  6. LeetCode第[29]题(Java):Divide Two Integers

    题目:两整数相除 难度:Medium 题目内容: Given two integers dividend and divisor, divide two integers without using ...

  7. spring:设置映射访问路径 或 xml配置访问路径 (spring mvc form表单)

    项目hello, 在src/main/java下面建一个目录: charpter2 一.xml配置访问路径 web.xml <web-app> <display-name>Ar ...

  8. yii2:doajax(post)会报500错误

    yii2:doajax(post)会报500错误:这是因为yii2开启了防御csrf的攻击机制,可去先去掉,在控制器里去掉:public $enableCsrfValidation = false , ...

  9. pOJ-1061 exgcd求同余方程组

    链接 就是求(m-n)*a+b*l=y-x, 类似于求解a*x+b*y=c,r=gcd(a,b),当c%r==0时有解,用exgcd求出a*x+b*y=gcd(a,b)的解,然后x*c/gcd(a,b ...

  10. Xcode 8 GM 编译缺失 /Users/usr/lib/libresolv.9.dylib

    原因是操作系统的文件与手机需要的不同. 解决办法是将iOS DeviceSupport里当前手机版本的Symbols的libresolv.9.dylib文件,代替编译失败项目的Build Phases ...