原题链接在这里: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. 浅析 MySQL Replication(本文转自网络)

    作者:卢飞 来源:DoDBA(mysqlcode) 0.导读 本文几乎涵盖了MySQL Replication(主从复制)的大部分知识点,包括Replication原理.binlog format.复 ...

  2. Book Review of “The practice of programming” (Ⅲ)

    The practice of programming Chapter 3 Design and Implementation In this section, we focus on one kin ...

  3. Spring AOP(4)

  4. hdu 2126 Buy the souvenirs 二维01背包方案总数

    Buy the souvenirs Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  5. Hibernate与JDBC、EJB、JDO的比较

    常用的数据库操作包括:JDBC.EJB.JDO以及Hibernate.它的各有优缺点: (1) JDBC:多数Java开发人员是用JDBC来和数据库进行通信,它可以通过DAO模式进行改善和提高.但这种 ...

  6. css滚动相关问题记录

    1) 关于滑动加速优化,可以通过css进行处理 例如,html如下: <div class="content-dialog"> <h1>活动规则</h ...

  7. web项目在iis配置好后不能正确访问问题集锦,以及IIS常规设置

    6.IIS配置好,项目无法访问,注意项目对应的应用程序池的net版本是否正确,是否集成(一般都是集成,很少是经典) 本项目用的4.0,可IIS默认程序池为2.0,将2.0改为4.0就行.   7.HT ...

  8. spring boot MongoDB的集成和使用

    前言 上一章节,简单讲解了如何集成Spring-data-jpa.本章节,我们来看看如何集成NoSQL的Mongodb.mongodb是最早热门非关系数据库的之一,使用也比较普遍.最适合来存储一些非结 ...

  9. Spring属性注入、构造方法注入、工厂注入以及注入参数(转)

    Spring 是一个开源框架. Spring 为简化企业级应用开发而生(对比EJB2.0来说). 使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能.Spring ...

  10. Node大文件处理

    之前有个需求要将文件解析再处理,当时直接将整个文件内容读到内存中然后解析,也是没有考虑到大文件的问题,那么要如何解析大文件呢? 输入:文件的内容是多个json,按顺序排列 输出:解析后的json数据 ...