# 738. Monotone Increasing Digits

Given a non-negative integer `N`, find the largest number that is less than or equal to `N` with monotone increasing digits.

(Recall that an integer has  monotone increasing digits  if and only if each pair of adjacent digits `x` and `y` satisfy `x <= y`.)

Example 1:

``````Input: N = 10
Output: 9
``````

Example 2:

``````Input: N = 1234
Output: 1234
``````

Example 3:

``````Input: N = 332
Output: 299
``````

Note: `N` is an integer in the range `[0, 10^9]`.

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

Github 同步地址：

https://github.com/grandyang/leetcode/issues/738

Remove K Digits

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

https://leetcode.com/problems/monotone-increasing-digits/discuss/109811/Simple-and-very-short-C%2B%2B-solution

LeetCode All in One 题目讲解汇总(持续更新中…)

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation