ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

556. Next Greater Element III

2019-04-22 21:50:36  阅读:351  来源: 互联网

标签:digits digit Greater nums 556 number smallest III greater


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

 

Approach #1: String. [Java]

class Solution {
    public int nextGreaterElement(int n) {
        int i, j;
        char[] nums = new String(n + "").toCharArray();
        for (i = nums.length-1; i > 0; --i) {
            if (nums[i-1] < nums[i])
                break;
        }
        
        if (i == 0) return -1;
        
        int x = nums[i-1], smallest = i;
        for (j = i + 1; j < nums.length; ++j) {
            if (nums[j] > x && nums[j] < nums[smallest]) 
                smallest = j;
        }
        char temp = nums[i-1];
        nums[i-1] = nums[smallest];
        nums[smallest] = temp;
        Arrays.sort(nums, i, nums.length);
        long ret = Long.parseLong(new String(nums));
        
        return ret < Integer.MAX_VALUE ? (int)ret : -1;
    }
}

  

Analysis:

At first, let's look at the edge cases:

1. If all digits sorted in descending order, then output is always "Not Possible". Foe example 4321.

2. If all digits are sorted in ascending order, then we need to swap last two digits. For example, 1234.

3. For other cases, we need to process the number from rightmost side.

 

The main algorithm works in following steps:

1. Traverse the given number from rightmost digit, keep traversing till you find a digit which is smaller the given trversed digit. For example, if the input number is "534976", we stop at 4 because 4 is smaller than next digit 9. If we do not find such a digit, then outpit is "Not Possible".

2. Now search the right side of above found digit 'd' for the smallest digit greater than 'd'. For "534976", the right side of 4 contains "976". The smallest digit greater than 4 to 6.

3. Swap the above found two digits, we get 536974 in above example.

4. Now sort all digits form position next to 'd' to the end of number. The number that we get after sorting is the output. For above example, we sort digits in bold 53974. We get "536479" which is the next greater number for input 534976.

 

Reference:

https://leetcode.com/problems/next-greater-element-iii/discuss/101824/Simple-Java-solution-(4ms)-with-explanation.

 

标签:digits,digit,Greater,nums,556,number,smallest,III,greater
来源: https://www.cnblogs.com/ruruozhenhao/p/10753238.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有