LeetCode 738
class Solution {
public int monotoneIncreasingDigits(int n) {
StringBuilder sb = new StringBuilder(String.valueOf(n));
int index = sb.length();
if(checkUp(sb.toString(), index)){
return Integer.parseInt(sb.toString());
}
while (index > 1){
sb.replace(index-1, index, "9");
char c = sb.charAt(index - 2);
if(c == '0'){
index--;
continue;
}
sb.replace(index-2, index-1, String.valueOf(Character.digit(c, 10)-1));
if(checkUp(sb.toString(), index-1)){
return Integer.parseInt(sb.toString());
}
index--;
}
return Integer.parseInt(sb.toString());
}
public boolean checkUp(String s, int index){
for(int i = 1; i < index; i++){
if(s.charAt(i) < s.charAt(i-1)){
return false;
}
}
return true;
}
}