Java version:

class Solution { public int monotoneIncreasingDigits(int N) { String str = new StringBuilder(N+"").toString(); int[] nums = new int[str.length()]; for(int i=0;i<str.length();i++){ nums[i] = str.charAt(i)-'0'; } int find_index = -1; for(int i=0;i<nums.length-1;i++){ if(nums[i+1]<nums[i]){ find_index = i+1; break; } } if(find_index == -1){ return N; } find_index--; while(find_index>=0){ nums[find_index]--; if(find_index==0 || (find_index>0 && nums[find_index]>=nums[find_index-1])){ break; } find_index--; } for(int i=find_index+1;i<nums.length;i++){ nums[i] = 9; } int p = 0; while(p<nums.length && nums[p]==0){ p++; } int res = 0; System.out.println(Arrays.toString(nums)); while(p<nums.length){ res = res*10+nums[p]; p++; } return res; } }Monotone Increasing Digits