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. 从hash算法到java hashcode()

    转载 https://blog.csdn.net/Walk_er/article/details/74976146 hash算法是一个摘要算法(yy:描述性算法:可以给一个物体确切的描述,但是不能通过 ...

  2. 在win8 App中,StorageFile比Path更好用

    Skip the path: stick to the StorageFile: http://blogs.msdn.com/b/wsdevsol/archive/2012/12/05/stray-f ...

  3. vue.js实现购物车功能

    购物车是电商必备的功能,可以让用户一次性购买多个商品,常见的购物车实现方式有如下几种: 1. 用户更新购物车里的商品后,页面自动刷新. 2. 使用局部刷新功能,服务器端返回整个购物车的页面html 3 ...

  4. 2018.11.02 NOIP模拟 距离(斜率优化dp)

    传送门 分四个方向分别讨论. 每次枚举当前行iii,然后对于第二维jjj用斜率优化dpdpdp. f[i][j]=(j−k)2+mindisk2f[i][j]=(j-k)^2+mindis_k^2f[ ...

  5. Multiplexer

    definition  a device that selects one of several analog or digital input signals and forwards the se ...

  6. vmware虚拟机centOs安装教程

    1安装vmware 虚拟机软件 1.解压vmware安装 汉化vmware虚拟机 复制注册码,并填写进vmware 2安装linux(centos)虚拟机 1.  点击文件----->新建虚拟机 ...

  7. i2c总线,核心,驱动详解

    Linux I2C驱动分析(一)----I2C架构和总线驱动 一.I2C总线原理 I2C是一种常用的串行总线,由串行数据线SDA 和串线时钟线SCL组成.I2C是一种多主机控制总线,它和USB总线不同 ...

  8. laravel 5.1 简单配置例子

    这里演示5.1版本 一.数据库配置 .env文件(也可以直接修改config/database.php) DB_HOST=localhost DB_DATABASE=test  //数据库名称 DB_ ...

  9. Struts2-result

    所有result-type <result-types> <result-type name="chain" class="com.opensympho ...

  10. java web 怎么下载大文件(上百M)

    Java代码   ; ]; , )) != -) { , bytesRead); 13.               } 14.               toClient.write(buffer ...