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 ...
随机推荐
- 服务端Job调度程序
GIT地址:https://github.com/youbl/PlanServer
- Win32 API编程:显示系统进程列表
#include <windows.h> #include <tlhelp32.h> // 声明快照函数的头文件 #include "tchar.h" #i ...
- jquery中ajax回调函数使用this
今天在写ajax请求的的时候success中代码老是不能正常执行,找了半天的原因,代码如下: 1 $.ajax({type: 'GET', 2 url: url, 3 data: oData, 4 s ...
- Hibernate常见优化策略
① 制定合理的缓存策略(二级缓存.查询缓存). ② 采用合理的Session管理机制. ③ 尽量使用延迟加载特性. ④ 设定合理的批处理参数. ⑤ 如果可以,选用UUID作为主键生成器. ⑥ 如果可以 ...
- java-四则运算-五-网页版--with刘童格
<%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncodin ...
- LeetCode第[66]题(Java):Plus One
题目:数组加一 难度:Easy 题目内容: Given a non-empty array of digits representing a non-negative integer, plus ...
- Linux软件安装(二)
1. 安装软件时,如果依赖的文件是 .so 类型的文件(so文件是谋个文件的小功能模块,如果php.ini中设置的模块引用就是 .so文件) ,这时被依赖的软件要安装完整的软件,一般可以根据 .so ...
- 英语发音规则---A字母
英语发音规则---A字母 一.总结 一句话总结:本文所有//的音标为英音音标,[]的音标为美音音标 1.A在开音节中发/eɪ/ [e]? age /eɪdʒ/ [edʒ] 年龄 ape /eɪp/ [ ...
- 影响wifi信号强度因素
影响WIFI信号强度因素: 1.室内错综复杂的环境,会在一定程度上导致WIFI无线信号产生多径传播现象,从而导致信号强度的不稳定性: 2.室内的人员密集程度和人员流动情况也会对WIFI信号强度产生影响 ...
- 【scala】元组
元组跟list类似,元组也是不可边的,但是元组可以容纳不同类型的元素. 元组用起来很简单,要实例化一个新的元组,只需要将对象放在圆括号当中,用逗号隔开即可. val pair = (99,“Luftb ...