556. Next Greater Element III
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: 21Example 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:
556. Next Greater Element III的更多相关文章
- 【LeetCode】556. Next Greater Element III 解题报告(Python)
[LeetCode]556. Next Greater Element III 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...
- [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 ...
- 556. Next Greater Element III下一个更大的数字
[抄题]: Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exac ...
- 496. Next Greater Element I + 503. Next Greater Element II + 556. Next Greater Element III
▶ 给定一个数组与它的一个子列,对于数组中的一个元素,定义它右边第一个比他大的元素称为他的后继,求所给子列的后继构成的数组 ▶ 第 496 题,规定数组最后一个元素即数组最大元素的后继均为 -1 ● ...
- LeetCode 556. 下一个更大元素 III(Next Greater Element III)
556. 下一个更大元素 III 556. Next Greater Element III 题目描述 给定一个 32 位正整数 n,你需要找到最小的 32 位整数,其与 n 中存在的位数完全相同,并 ...
- [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 ...
- LeetCode Next Greater Element III
原题链接在这里:https://leetcode.com/problems/next-greater-element-iii/description/ 题目: Given a positive 32- ...
- [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 ...
- [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 ...
随机推荐
- JS closure
闭包的概念 闭包,不同于一般的函数,它允许一个函数在立即词法作用域外调用时,仍可访问非本地变量. --维基百科 闭包就是能够读取其他函数内部变量的函数. --阮一峰 由于在Javascript语言中, ...
- IDEA如何把写好的java文件/项目打包成一个jar的文件
一.命令行的方法 打开cmd,输入jar -cvf [打包后的文件名].jar [要打包的目录]. 二.IDEA的方法 写完一个java程序把它封装成一个jar的包 这样就可以在别的jar上面运行这 ...
- php中如何解决显示数据库中的内容乱码
第一步: 第二步:在代码开始处加一行
- css扩大超链接的点击范围
给a标签设置display:block后,它会根据盒模型计算a标签的实际大小.这时候,你可以使用hover伪类使a的整个盒模型生效..link a{display:block;padding:2px ...
- springboot深入学习(四)-----tomcat配置、websocket
一.更改servlet服务器 springboot中默认可以集成多种servlet容器,当引入如下依赖时: springboot默认以tomcat作为项目的servlet容器,如果用户想要替换tomc ...
- c语言const和c++const
1.常量 常量是指值不能被改变的量,又叫做字面值 1.1常量分类 1)字符常量:'a', 'A', '*'. 2)字符串常量:"helloworld","ilovechi ...
- 分分钟搞懂union与union all
SQL UNION 操作符 UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每 ...
- winform 可拖动无边框窗体解决办法
方法一:通过重载消息处理实现. 鼠标的拖动只对窗体本身有效,不能在窗体上的控件区域点击拖动 /// <summary> /// 通过重载消息处理实现.重写窗口过程(WndProc),处理一 ...
- java项目显示红叉,程序却没有错误
转 http://blog.sina.com.cn/s/blog_825b7d7c0102w7rq.html (2016-07-02 11:38:38) 分类: javaWeb 电脑换了不同版本的 ...
- asp.net文件上传下载
泽优大文件上传产品测试 泽优大文件上传控件up6,基于php开发环境测试. 开发环境:HBuilder 服务器:wamp64 数据库:mysql 可视化数据库编辑工具:Navicat Premium ...