Monotone Increasing Digits LeetCode solution in Java
1class Solution {2public int monotoneIncreasingDigits(int n) {3int position;4int digitInTheNextPosition;5while ((position = getThePositionNotSatisfied(n)) != -1) {6digitInTheNextPosition = ((int) (n / Math.pow(10, position - 1))) % 10;7n -= Math.pow(10, position - 1) * (digitInTheNextPosition + 1);8n -= n % Math.pow(10, position);9n += Math.pow(10, position) - 1;10}11return n;12}1314public int getThePositionNotSatisfied(int n) {15int k = 10;16int position = 0;17while (n > 0) {18if (k < n % 10) {19return position;20} else {21k = n % 10;22n /= 10;23position++;24}25}26return -1;27}28}