原题链接在这里: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. Python面试题之Python对象反射、类反射、模块反射

    python面向对象中的反射:通过字符串的形式操作对象相关的属性.python中的一切事物都是对象(都可以使用反射) 一.getattr 对象获取 class Manager: role = &quo ...

  2. Sybase:解锁

    Sybase:解锁 Sql代码: --查询锁表 sp_iqlocks --解除锁定 drop connection[连接序号]

  3. CentOS 7如何将.deb文件转换.rpm

    1.首先下载alien工具 http://ftp.de.debian.org/debian/pool/main/a/alien/  http://ftp.de.debian.org/debian/po ...

  4. cookie 与 session 的区别详解

    [转]cookie 与session 的区别详解 二者的定义: 当你在浏览网站的时候,WEB 服务器会先送一小小资料放在你的计算机上,Cookie 会帮你在网站上所打的文字或是一些选择,都纪录下来.当 ...

  5. ThinkPHP关于模板的一些嵌套、IF判断使用

    > ##### 前言,现在有一组数据(涉及到3个数据表,order订单表,order_process办理流程表,process_status流程描述表),根据当前订单,展示相应信息 1.办理流程 ...

  6. 浅谈MySQL中优化sql语句查询常用的30种方法 - 转载

    浅谈MySQL中优化sql语句查询常用的30种方法 1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中使 ...

  7. html checkbox多选框语法与结构

    <input name="Fruit" type="checkbox" value="" /> 用法用例 <foreach ...

  8. Android--第三方控件--okHttp

    Android中有很多的第三方控件,其中OkHttp是一个很强大的用于网络加载的第三方框架,当然了,它的内部也是使用原生的代码封装好的.今天我们就来看一下OkHttp的简单用法: 说到网络请求,肯定就 ...

  9. Java条件语句之 if

    生活中,我们经常需要先做判断,然后才决定是否要做某件事情.例如,如果考试成绩大于 90 分,则奖励一个 IPHONE 5S .对于这种“需要先判断条件,条件满足后才执行的情况”,就可以使用if 条件语 ...

  10. docker 跨主机网络:overlay 简介

    简介 docker 在早前的时候没有考虑跨主机的容器通信,这个特性直到 docker 1.9 才出现.在此之前,如果希望位于不同主机的容器能够通信,一般有几种方法: 使用端口映射:直接把容器的服务端口 ...