class Solution {
public int monotoneIncreasingDigits(int n) {
int[] res = getNum(n);
if(res.length == 1) return res[0];
for(int i = 0; i < res.length - 1;i++){
if(res[i] < res[i + 1]){
for(int j = i; j >= 0;j--) res[j] = 9;
res[i + 1]--;
}
}
int ans = 0;
for(int i = 0; i < res.length;i++){
ans = ans + res[i] *(int) Math.pow(10,i);
}
return ans;
}
public int[] getNum(int n){
int count = 0;
int m = n;
while(m > 0){
m /= 10;
count++;
}
int[] res= new int[count];
count = 0;
while(n > 0){
res[count++] = n%10;
n = n/10;
}
return res;
}
}
简单,从个位开始向后比较,如果出现res[i] < res[i+1],则将res[i+]--,退一位将前面所有的位数都变为999.