556. Next Greater Element III

跟字典序那道题一样 找出下一个 大于MAX_VALUE或者相同就return-1

 1 class Solution {
 2     public int nextGreaterElement(int n) {
 3         if(n == 0) return -1;
 4         String str = "" + n;
 5         char[] arr = str.toCharArray();
 6         int[] arr1 = new int[arr.length];
 7         for(int i = 0; i < arr.length; i++){
 8             arr1[i] = arr[i] - ‘0‘;
 9         }
10         int i;
11         for(i = arr.length - 1; i > 0; i--){
12             if(arr1[i-1] < arr1[i]){
13                 break;
14             }
15         }
16         if(i != 0){
17             swap(arr1, i-1);
18         }else{
19             return -1;
20         }
21
22         reverse(arr1, i);
23         String res = "";
24         for(i = 0; i < arr1.length; i++){
25             res += arr1[i];
26         }
27         long res1 = Long.parseLong(res);
28         return (res1 > Integer.MAX_VALUE) ? -1 : (int)res1;
29
30     }
31
32     public void swap(int[] arr1, int n){
33         for(int i = arr1.length - 1; i > n; i--){
34             if(arr1[i] > arr1[n]){
35                 int temp = arr1[n];
36                 arr1[n] = arr1[i];
37                 arr1[i] = temp;
38                 break;
39             }
40         }
41     }
42
43     public void reverse(int[] arr1, int i ){
44         int last = arr1.length - 1;
45         while(i < last){
46             int temp = arr1[i];
47             arr1[i] = arr1[last];
48             arr1[last] = temp;
49             i++;
50             last--;
51         }
52     }
53 }

原文地址:https://www.cnblogs.com/goPanama/p/9762625.html

时间: 2024-10-21 07:36:09

556. Next Greater Element III的相关文章

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. Exampl

LeetCode 556. 下一个更大元素 III(Next Greater Element III)

556. 下一个更大元素 III 556. Next Greater Element III 题目描述 给定一个 32 位正整数 n,你需要找到最小的 32 位整数,其与 n 中存在的位数完全相同,并且其值大于 n.如果不存在这样的 32 位整数,则返回-1. LeetCode556. Next Greater Element III中等 示例 1: 输入: 12 输出: 21 示例 2: 输入: 21 输出: -1 示例 3: 输入: 12443322 输出: 13222344 Java 实现

leetcode556—— Next Greater Element III (JAVA)

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: I

[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 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: I

[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 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: In

LeetCode 496. 下一个更大元素 I(Next Greater Element I) 35

496. 下一个更大元素 I 496. Next Greater Element I 题目描述 给定两个没有重复元素的数组?nums1 和?nums2,其中 nums1?是?nums2?的子集.找到?nums1?中每个元素在?nums2?中的下一个比其大的值. nums1?中数字?x?的下一个更大元素是指?x?在?nums2?中对应位置的右边的第一个比?x?大的元素.如果不存在,对应位置输出 -1. 每日一算法2019/6/7Day 35LeetCode496. Next Greater Ele

数组和矩阵(3)——Next Greater Element I

https://leetcode.com/problems/next-greater-element-i/#/description You are given two arrays (without duplicates) nums1 and nums2 where nums1's elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding p

496. Next Greater Element I 另一个数组中对应的更大元素

[抄题]: You are given two arrays (without duplicates) nums1 and nums2 where nums1's elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2. The Next Greater Number of a number x in nums

[leetcode] Next Greater Element

今天处理一下一系列题目:Next Greater Element系列. Next Greater Element I You are given two arrays (without duplicates) nums1 and nums2 where nums1's elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of