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 n and 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

Approach #1: String. [Java]

class Solution {
public int nextGreaterElement(int n) {
int i, j;
char[] nums = new String(n + "").toCharArray();
for (i = nums.length-1; i > 0; --i) {
if (nums[i-1] < nums[i])
break;
} if (i == 0) return -1; int x = nums[i-1], smallest = i;
for (j = i + 1; j < nums.length; ++j) {
if (nums[j] > x && nums[j] < nums[smallest])
smallest = j;
}
char temp = nums[i-1];
nums[i-1] = nums[smallest];
nums[smallest] = temp;
Arrays.sort(nums, i, nums.length);
long ret = Long.parseLong(new String(nums)); return ret < Integer.MAX_VALUE ? (int)ret : -1;
}
}

  

Analysis:

At first, let's look at the edge cases:

1. If all digits sorted in descending order, then output is always "Not Possible". Foe example 4321.

2. If all digits are sorted in ascending order, then we need to swap last two digits. For example, 1234.

3. For other cases, we need to process the number from rightmost side.

The main algorithm works in following steps:

1. Traverse the given number from rightmost digit, keep traversing till you find a digit which is smaller the given trversed digit. For example, if the input number is "534976", we stop at 4 because 4 is smaller than next digit 9. If we do not find such a digit, then outpit is "Not Possible".

2. Now search the right side of above found digit 'd' for the smallest digit greater than 'd'. For "534976", the right side of 4 contains "976". The smallest digit greater than 4 to 6.

3. Swap the above found two digits, we get 536974 in above example.

4. Now sort all digits form position next to 'd' to the end of number. The number that we get after sorting is the output. For above example, we sort digits in bold 53974. We get "536479" which is the next greater number for input 534976.

Reference:

https://leetcode.com/problems/next-greater-element-iii/discuss/101824/Simple-Java-solution-(4ms)-with-explanation.

556. Next Greater Element III的更多相关文章

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

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

  2. [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 ...

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

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

  4. 496. Next Greater Element I + 503. Next Greater Element II + 556. Next Greater Element III

    ▶ 给定一个数组与它的一个子列,对于数组中的一个元素,定义它右边第一个比他大的元素称为他的后继,求所给子列的后继构成的数组 ▶ 第 496 题,规定数组最后一个元素即数组最大元素的后继均为 -1 ● ...

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

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

  6. [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 ...

  7. LeetCode Next Greater Element III

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

  8. [leetcode-556-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. [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 ...

随机推荐

  1. SpringMVC学习笔记:表单提交 参数的接收

    SpringMVC可以接收原生form表单和json格式数据 有一个名为Book的model,其中的属性如下: 字符串类型的name,数字类型的price,数组类型的cover,集合类型的author ...

  2. 【转】四、可空类型Nullable<T>到底是什么鬼

    [转]四.可空类型Nullable<T>到底是什么鬼 值类型为什么不可以为空 首先我们都知道引用类型默认值都是null,而值类型的默认值都有非null. 为什么引用类型可以为空?因为引用类 ...

  3. DockerDesktop简单安装和使用

    一.在windows10下,安装DockerDesktop: 1.检查windows版本为企业版或专业版,并开启Hyper-v系统设置:电脑的控制面板->程序->启用或关闭Windows功 ...

  4. 【Java】生成图形验证码

    本章介绍一个能生成比较好看的图形验证码类 生成验证码工具类 package com.util; import java.awt.Color; import java.awt.Font; import ...

  5. KM匹配板子

    /* gyt Live up to every day */ #include<cstdio> #include<cmath> #include<iostream> ...

  6. unity延时函数

    新建一个工具类 public class DelayToInvoke : MonoBehaviour{ public static IEnumerator DelayToInvokeDo(Action ...

  7. zend studio导入外部项目乱码怎么解决

    在zendstudio ide中,导入一个工程后,发现工程里面很多的文件都打上了红色的叉叉,打开这些文件一看,发现只要是有汉字存在的文件,都出现了乱码.按住alt+enter发现,该文件的编码默认为g ...

  8. 三个UID

    1.三个UID 这三个UID分别是实际用户ID(real uid).有效用户ID(effective uid).保存的设置用户ID(saved set-user-ID)(SUID) 实际用户ID(RU ...

  9. shell脚本之正则表达式

    具体参考: www.jb51.net/tools/shell_regex.html 正则表达式常用于grep AWK 等工具中

  10. JavaScript常用定义和方法

    1.字符串一些常用方法,注意,调用这些方法本身不会改变原有字符串的内容,而是返回一个新字符串.toUpperCase()把一个字符串全部变为大写: var s = 'Hello'; s.toUpper ...