1. 程式人生 > >leetcode738+構造不大於n的單調遞增數字,貪心

leetcode738+構造不大於n的單調遞增數字,貪心

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);
    }
};