Leetcode-670:maximum Swap (the largest integer of the interchange number)--Medium

Source: Internet
Author: User
Tags split
Question

Given a non-negative integer, you could swap both digits at most once to get the maximum valued number. Return the maximum valued number you could get.

Example 1:

input:2736 output:7236 Explanation:swap The number 2 and the number
7.

Example 2:

input:9973
output:9973
explanation:no swap.

Note:
* The given number is in the range [0, 108].

Problem Resolution:

Given a non-null non-negative integer, the maximum number of the two digits is exchanged, and the largest integer is returned. Answer Solution 1:

Personal solutions: First, with an example to analyze the features to be exchanged, we will find that when all the numbers in an integer are sorted in a non-ascending order, the integer is the largest integer and does not need to be exchanged; So according to the above analysis, we need to look for the number in the integer that does not conform to the non-increment rule, Record the breaking point of the breach, search for the largest number in the second half of the split point, and the higher the position of the number relative to the front of the previous number of equal to the significance of the exchange, that is, we ask for Digits[j] >= Max, note this is >=; The first half of the split point is looking forward to the maximum value of Max, which is less than the previous step, and the largest integer is exchanged by swapping the two position numbers found. Note that in order to do this, the integer is converted to a char array and transformed back to an integer after the interchange.

Class Solution {public int maximumswap (int num) {char[] digits = integer.tostring (num). ToCharArray ();

        if (digits.length = = 1) return num;
        Look for lines that do not conform to the non-ascending order int split = 0;
                for (int i = 0; i < digits.length-1; i++) {if (Digits[i] < digits[i+1]) {split = i+1;
            Break
        }}//Find the maximum value in the section behind the dividing line max char max = digits[split];
        int index1 = split;
                for (int j = split+1; J < Digits.length; J + +) {if (Digits[j] >= max) {max = digits[j];
            Index1 = j;
        }}//In front of the dividing line, look forward to the maximum value of the max int index2 = split;
            for (int i = split-1; I >= 0, i--) {if (Digits[i] >= max) {break;
        } index2--;
        }//Exchange two bits found char char temp = digits[index1];
        DIGITS[INDEX1] = Digits[index2]; DIGITS[INDEX2] = Temp
    Return integer.valueof (new String (digits)); }
}
Complexity of Time: O (n), spatial complexity: O (n) Solution 2:

The idea of using buckets. Use the indices bucket array to record the last position of the digital 0〜9. Iterates through an array of integers into char, extracts each number from left to right, and checks to see if there is a larger number in each of the extracted numbers, so it is compared to the position number of the bucket (starting from 9 to the current number), if there is a larger bucket than the extracted number, We also need to make sure that the position of the larger number is behind the bucket number, and if it is found, swap two positions, transform the array and return. This solution is more ingenious.

Class Solution {public
    int maximumswap (int num) {
        char[] digits = integer.tostring (num). ToCharArray ();
        int[] Indices = new INT[10];
        int result = num;
        for (int i = 0; i < digits.length; i++) {
            indices[digits[i]-' 0 '] = i;
        }
        for (int i = 0; i < digits.length-1; i++) {
            int digit = digits[i]-' 0 ';
            for (int j = 9; j > Digit; j--) {
                if (Indices[j] > i) {
                    char temp = digits[i];
                    Digits[i] = digits[indices[j]];
                    DIGITS[INDICES[J]] = temp;
                    result = Integer.parseint (new String (digits));
                    return result;}}
        }
        return result;
    }
}
Complexity of Time: O (n), spatial complexity: O (n)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.