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. Fitnesse初体验

    FitNesse是一套软件开发协作工具 FitNesse是帮助大家加强软件开发过程中的协作的工具.能够让客户.测试人员和开发人员了解软件要做成什么样,帮助建议软件最终是否达到了设计初衷. 什么是Fit ...

  2. asp导航条子菜单横向

    示意图:(代码红色部分为主要.) <%@ Master Language="C#" AutoEventWireup="true" CodeFile=&qu ...

  3. windows10+VS+CUDA+cuDNN+TensorFlow-gpu环境搭建(问题及解决)

    TensorFlow-gpu环境需要CUDA+cuDNN+python,CUDA又需要VS,所以,,,环境越来越大哈哈. 1.主要环境: Python 3.6 CUDA9.0 Cudann7.0 Te ...

  4. python2.7.X 升级至Python3.6.X

    安装Python3 项目是在py3环境下进行编码的,正好yczhang默认的py版本是2,我们还需要安装py3才能让程序run起来,在此之前,需要安装开发工具包,因为要编译安装Python [root ...

  5. 模板优化 运用 function 及 外部模板

    我们都知道模板是泛型的,但是,它一旦被实例化就会产生一个实例化的副本. 好了,大家应该能够猜到,低效模板和高效模板的差异了 一般的低效模板: 1.泛型实参表达形式多样导致的低效模板 2.多文件引用同一 ...

  6. Fiddler配置

    用fiddler来抓取手机app测试包的数据很方面,配置时需要注意一下几点: 1.保证电脑的防火墙是关闭的,不然是会抓不到包的: 2.查看下fiddler的默认端口8888是否被占用,如果被占用了,那 ...

  7. 【bzoj5084】 hashit(广义SAM+set)

    题面 传送门 题解 后缀平衡树是个啥啊我不会啊-- 那么我们来考虑一下\(SAM\)的做法好了 不难发现它的本义是要我们维护一棵\(trie\)树,并求出\(trie\)树上每一个节点到根的这段串的不 ...

  8. python数据转换工具Into

    转:http://python.jobbole.com/81564/ 再次介绍Into包:整洁地数据迁移 2015/03/12 ·  基础知识 ·  into, 数据迁移 分享到:3 本文由 伯乐在线 ...

  9. 51nod1282(最小表示法&&枚举)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1282 题意:中文题目诶- 思路:指针不可转,刻盘可转,显然,对 ...

  10. powerDesignner连接数据库

    http://hi.baidu.com/huntererpang/item/e65e1c48aa0ab50a6dc2f090 选用microsoft odbc for oracle 数据源名称:我喜欢 ...