-
Notifications
You must be signed in to change notification settings - Fork 46
/
_738.java
40 lines (38 loc) · 985 Bytes
/
_738.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* LeetCode 738 - Monotone Increasing Digits
* <p>
* Greedy
* <p>
* I think the following two test cases should give enough hints:
* <p>
* Input: N = 332
* Output: 299
* <p>
* Input: N = 100
* Output: 99
*/
public class _738 {
public int monotoneIncreasingDigits(int N) {
char[] s = ("" + N).toCharArray();
for (int i = 0; i < s.length - 1; i++) {
if (s[i] > s[i + 1]) {
char val = s[i];
for (int j = i; j >= 0; j--)
if (s[j] == val) {
s[j] = (char) (val - 1);
if (j + 1 < s.length) {
s[j + 1] = '9';
}
}
for (int j = i + 1; j < s.length; j++)
s[j] = '9';
break;
}
}
int ans = 0;
for (char ch : s) {
ans = ans * 10 + ch - '0';
}
return ans;
}
}