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.
该博客介绍了一个Java实现的算法,用于找到一个整数的单调递增子序列。当遇到数字序列中某个数字小于其后继数字时,该算法会将后继数字减一,并将所有后续数字置为9,以保持单调递增。算法首先获取输入数字的每一位,然后进行处理,最后将处理后的数字转换回十进制形式返回。
183

被折叠的 条评论
为什么被折叠?



