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的单调递增数字,贪心

相关文章:

  • 2021-10-09
  • 2021-11-08
  • 2021-12-28
  • 2022-12-23
  • 2022-01-17
  • 2021-10-22
  • 2021-09-05
  • 2021-12-15
猜你喜欢
  • 2021-12-31
  • 2022-12-23
  • 2022-12-23
  • 2021-06-30
  • 2023-03-21
  • 2021-07-12
相关资源
相似解决方案