-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSolution738.java
More file actions
35 lines (27 loc) · 860 Bytes
/
Solution738.java
File metadata and controls
35 lines (27 loc) · 860 Bytes
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
package algorithm.leetcode;
/**
* @author: mayuan
* @desc: 单调递增的数字
* @date: 2019/01/22
*/
public class Solution738 {
public static void main(String[] args) {
final int n = 332;
System.out.println(new Solution738().monotoneIncreasingDigits(n));
}
public int monotoneIncreasingDigits(int N) {
char[] number = String.valueOf(N).toCharArray();
// 必须从后向前推导,才能确保最前面几个数字是递增的
int mark = number.length;
for (int i = number.length - 1; i > 0; --i) {
if (number[i] < number[i - 1]) {
mark = i - 1;
--number[mark];
}
}
for (int i = mark + 1; i < number.length; ++i) {
number[i] = '9';
}
return Integer.parseInt(new String(number));
}
}