738.单调递增的数字
题目链接:738. 单调递增的数字 - 力扣(LeetCode)
class Solution {public int monotoneIncreasingDigits(int n) {int[] a = new int[11];int k = 0;for(int i = 0; n != 0; ++i) {a[i] = n % 10;n /= 10;++k;}if(k == 1) {return n;}for(int i = k-1; i > 0; --i) {if(a[i] <= a[i-1]) {continue;}else {while(a[i] == a[i+1]) {++i;}--a[i];--i;while(i >= 0) {a[i] = 9;--i;}}}int z = 1,ret = 0;for(int i = 0; i < k; ++i) {ret += a[i] *z;z *= 10;}return ret;}
}