[Java] 738. Monotonically increasing numbers! ! !

Given a non-negative integer N, find the largest integer less than or equal to N, and at the same time this integer needs to satisfy that the number of each digit is monotonically increasing.

(If and only if the numbers x and y on each adjacent digit satisfy x <= y, we call this integer monotonically increasing.)

Example 1:

Input: N = 10
Output: 9
Example 2:

Input: N = 1234
Output: 1234
Example 3:

Input: N = 332
Output: 299

public int monotoneIncreasingDigits(int N) {
    
    
        int i = 1;
        int res = N;
        while( i <= res/10) {
    
    
            int temp = res / i % 100;
            i *= 10;
            if(temp / 10 > temp % 10) 
                res = res / i * i - 1;
        }
        return res;
    }

Guess you like

Origin blog.csdn.net/qq_44461217/article/details/111191445