0738.cpp (428B)
1 class Solution { 2 public: 3 int monotoneIncreasingDigits(int n) const { 4 string num = to_string(n); 5 const int m = size(num); 6 int stop = m - 1; 7 8 for (int i = m - 1; i > 0; i--) { 9 if (num[i] >= num[i - 1]) continue; 10 stop = i - 1; 11 num[i - 1]--; 12 } 13 14 for (int i = stop + 1; i < m; i++) 15 num[i] = '9'; 16 17 return stoi(num); 18 } 19 };