LeetCode Next Greater Element III
原题链接在这里: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 Permutation, Next Greater Element I, Next Greater Element II.
LeetCode Next Greater Element III的更多相关文章
- [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 556. 下一个更大元素 III(Next Greater Element III)
556. 下一个更大元素 III 556. Next Greater Element III 题目描述 给定一个 32 位正整数 n,你需要找到最小的 32 位整数,其与 n 中存在的位数完全相同,并 ...
- 【LeetCode】556. Next Greater Element III 解题报告(Python)
[LeetCode]556. Next Greater Element III 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...
- [LeetCode] Next Greater Element II 下一个较大的元素之二
Given a circular array (the next element of the last element is the first element of the array), pri ...
- [LeetCode] Next Greater Element I 下一个较大的元素之一
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...
- LeetCode——Next Greater Element I
LeetCode--Next Greater Element I Question You are given two arrays (without duplicates) nums1 and nu ...
- [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 ...
- [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 ...
- 556. Next Greater Element III下一个更大的数字
[抄题]: Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exac ...
随机推荐
- Vmware ESXi 6.0 多Vlan部署,vSphere Client管理方法
背景: 公司IT部门新购了两台服务器与一台存储,打算做虚拟化,并将存储分成两个部分,分别配给那两台服务器.在宿主机上要安装的虚拟机属于不同的网段,这就涉及了多VLAN,当然这并不是多么高深的技术,属于 ...
- Google揭露SHA-1碰撞,加速数据重删字节对比
原创 架构师技术联盟 近期,Google和道荷兰阿姆斯特研究者宣布攻破了世界上第一例公开的SHA-1哈希碰撞实例,业界一片哗然.当两组不同的数据(文件.一段数据)计算出相同的Hash值时,即视为二者 ...
- cocoa中获得root权限的几种方法
目前我所知道的,在cocoa中获得root权限的方法有3种: 1. 通过AuthorizationCopyRights函数 2. 在UI上添加一个锁的样子的控件,然后通过开关这个锁来获取root权限 ...
- lamp架构之升级php版本
当你看到这篇文章的时候 YHSPY.COM 服务器上的PHP版本已经从 5.4.27 升级到了 7.0.4,这是一个重大的飞跃.一路升级遇到了很多问题.官方声称PHP7最大的升级就是在语言性能上的提升 ...
- centos、linux查找未挂载磁盘格式化并挂载?
centos.linux查找未挂载磁盘格式化并挂载? df -h 查看当前linux服务器硬盘: fdisk -l /dev/sda 第一块硬盘 /dev/sdb 第二块硬盘 依此类推 以/d ...
- Registering Components-->Autofac registration(include constructor injection)
https://autofaccn.readthedocs.io/en/latest/register/registration.html Registration Concepts (有4种方式来 ...
- BOOST编译方法
Windowsbjam --toolset=msvc-9.0 --prefix=C:\vc9_boost\vc9 --build-type=complete link=static threading ...
- linux rpm 卸载,简单说明
平时Linux卸载文件总是遇到卸载不干净,各种依赖什么的,今天又是搞这玩意,就记录下一个比较常规的方法. 一.查询包括某关键字的软件(这里以卸载openoffice为例) 查询包括office的软件 ...
- Python 导出数据from Mysql
环境 Anaconda3 Python 3.6, Window 64bit 目的 从MySQL数据库读取目标表数据,并处理 代码 # -*- coding: utf-8 -*- import pand ...
- 用phpexcel插件导出excel2003
ob_end_clean();//清空缓冲区并关闭输出缓冲(清除脏数据). header('Content-Type:application/vnd.ms-execel'); header('Cont ...