zoukankan      html  css  js  c++  java
  • 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: Input: 12 Output: 21 Example 2: Input: 21 Output: -1

    This solution is just a java version derived from this post.

    At first, lets look at the edge cases -

    1. If all digits sorted in descending order, then output is always “Not Possible”. For example, 4321.
    1. If all digits are sorted in ascending order, then we need to swap last two digits. For example, 1234.
    2. For other cases, we need to process the number from rightmost side (why? because we need to find the smallest of all greater numbers)

    Now the main algorithm works in following steps -

    I) Traverse the given number from rightmost digit, keep traversing till you find a digit which is smaller than the previously traversed 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 output is “Not Possible”.

    II) 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 is 6.

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

    IV) Now sort all digits from 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 536974. We get “536479” which is the next greater number for input 534976.

    public int nextGreaterElement(int n) {
            char[] number = (n + "").toCharArray();
            
            int i, j;
            // I) Start from the right most digit and 
            // find the first digit that is
            // smaller than the digit next to it.
            for (i = number.length-1; i > 0; i--)
                if (number[i-1] < number[i])
                   break;
    
            // If no such digit is found, its the edge case 1.
            if (i == 0)
                return -1;
                
             // II) Find the smallest digit on right side of (i-1)'th 
             // digit that is greater than number[i-1]
            int x = number[i-1], smallest = i;
            for (j = i+1; j < number.length; j++)
                if (number[j] > x)
                    smallest = j;
            
            // III) Swap the above found smallest digit with 
            // number[i-1]
            char temp = number[i-1];
            number[i-1] = number[smallest];
            number[smallest] = temp;
            
            // IV) Sort the digits after (i-1) in ascending order
            Arrays.sort(number, i, number.length);
            
            long val = Long.parseLong(new String(number));
            return (val <= Integer.MAX_VALUE) ? (int) val : -1;
        }
    

      

  • 相关阅读:
    SNF软件开发机器人-子系统-功能-启用大按钮样式如何配置
    SNF软件开发机器人-子系统-功能-数据录入方式
    SNF软件开发机器人-子系统-功能-功能类型(普通表改为树型表)
    SNF软件开发机器人-子系统-导出-导入功能-多人合作时这个功能经常用到
    SQL SERVER数据库删除LOG文件和清空日志的方案
    打不开磁盘“I:xxx.vmdk”或它所依赖的某个快照磁盘
    chrome浏览器解决跨域问题
    AngularJS判断checkbox/复选框是否选中并实时显示
    非常全的VsCode快捷键
    Oracle&SQLServer中实现跨库查询
  • 原文地址:https://www.cnblogs.com/apanda009/p/7675286.html
Copyright © 2011-2022 走看看