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 ...
 
随机推荐
- jQuery垂直手风琴菜单 菜单项带小图标
			
在线演示 本地下载
 - 迁移cnblog博客
			
title: 迁移cnblog博客 date: 2018-03-18 categories: cnblogs tags: life 写博客最怕就是这种迁来迁去的,太麻烦了 还好老早就使用markdow ...
 - SSO 证书配置
			
ssodev.crt为开发环境证书ssotest.crt为测试环境证书 将证书拷贝到目录:{JDK}\jre\lib\security 其中 {JDK} 是实际安装JDK的位置.然后cmd进入命令窗口 ...
 - 给定字符串数组,用map的key保存数组中字符串元素,value保存字符串元素出现次数,最后统计个字符串元素出现次数
			
import java.util.HashMap; public class map1 { public static void main(String[] args) { String[] arra ...
 - JNI简单步骤01
			
1.环境变量 1.1.相应的环境变量中,加入如下内容:(Windows) (1).ClASSPATH中输入 : ".;C:\Program Files\Java\jdk1.7.0_07\jr ...
 - 全国城市部分js
			
var areaJson22 = { "id": "0", "name": "全国", "parentId&q ...
 - django-simple-captcha 使用 以及添加动态ajax刷新验证
			
参考博客:http://blog.csdn.net/tanzuozhev/article/details/50458688?locationNum=2&fps=1 参考博客:http://bl ...
 - mysql中删除完全重复数据的准确SQL语句
			
删除数据库中重复的记录,只保留一条 DELETE FROM tb_gps_records WHERE id NOT IN (SELECT bid FROM (SELECT min(id) as bid ...
 - 用WebClient在异步下载或上传时每次只进行一个任务 C#
			
当在每次上传或者下载的时候,我只想进行一个任务的,我用的是WebClient类,但是我又不想用同步的方法UploadFile.DownloadFile,因为WebClient这个类的同步方法没有Upl ...
 - WSL安装xfce4
			
参考:https://github.com/Microsoft/WSL/issues/637 安装组件 1. win10 上安装 Xming https://sourceforge.net/proje ...