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:

Input: 12
Output: 21

Example 2:

Input: 21
Output: -1转载注明出处:http://www.cnblogs.com/wdfwolf3/,谢谢。时间复杂度O(n),空间复杂度O(n),5ms。
public int nextGreaterElement(int n){
        //如果是1位整数,直接返回-1,同时加上了10和11
        if(n <= 11){
            return -1;
        }
        //转化为char数组,方便处理数字
        char[] nums = (n+"").toCharArray();
        int i = nums.length - 2;
        //从后往前找到第一个升序的位数
        for (; i >= 0; i--) {
            if (nums[i] < nums[i+1]) {
                break;
            }
        }
        //如果没有即不存在,返回-1
        if(i < 0){
            return -1;
        }
        int j = nums.length -1;
        //从后往前查找第一个比i大的数字,这样找出来的是所有大于i数字中的最小值
        for (; j > i; j--) {
            if(nums[i] < nums[j]){
                break;
            }
        }
        //交换i,j位置的数字
        char tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
        //i之后的数字排序,让结果最小
        Arrays.sort(nums, i, nums.length);
        //有可能交换后越界,使用long类型判断一下
        long ans = Long.parseLong(new String(nums));
        return (ans>Integer.MAX_VALUE)?-1:((int)ans);
    }
 
时间: 2025-01-15 05:25:30

leetcode556—— Next Greater Element III (JAVA)的相关文章

LeetCode算法题-Next Greater Element I(Java实现)

这是悦乐书的第244次更新,第257篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第111题(顺位题号是496).你有两个数组(没有重复)nums1和nums2,其中nums1的元素是nums2的子集.在nums2的相应位置找到nums1元素的所有下一个更大的数字.nums1中的数字x的下一个更大数字是nums2中右边第一个更大的数字.如果它不存在,则输出该数字的-1.例如: 输入:nums1 = [4,1,2],nums2 = [1,3,4,2]. 输出:[-1,

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 实现

[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

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

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

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

【LeetCode-面试算法经典-Java实现】【216-Combination Sum III (组合数的和)】

[216-Combination Sum III (组合数的和)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 代码下载[https://github.com/Wang-Jun-Chao] 原题 Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination

数组和矩阵(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