https://leetcode.com/problems/monotone-increasing-digits/description/

class Solution {
public:
    int monotoneIncreasingDigits(int N) {
        string str = to_string(N);
        int n = str.size(), j = n;
        for(int i=n-1; i>0; i--){
            if(str[i]>=str[i-1]) continue;
            --str[i-1];
            j = i;
        }
        for(int i=j; i<n; i++){
            str[i] = '9';
        }
        return stoi(str);
    }
};

leetcode738+构造不大于n的单调递增数字,贪心

相关文章: