public class Solution
    {
        public int MonotoneIncreasingDigits(int N)
        {
            var num = N.ToString();
            var len = num.Length;
            if (len == 1)
            {
                return N;
            }
            int[] X = new int[len];
            for (int i = 0; i < len; i++)
            {
                X[i] = Convert.ToInt32(num[i].ToString());
            }
            var change = true;
            while (change)
            {
                var change_count = 0;
                for (int i = 1; i < len; i++)
                {
                    var n1 = X[i - 1];
                    var n2 = X[i];
                    if (n1 > n2)
                    {
                        change_count++;
                        X[i - 1] -= 1;
                        for (int j = i; j < len; j++)
                        {
                            X[j] = 9;
                        }
                        change = true;
                        break;
                    }
                }
                if (change_count == 0)
                {
                    change = false;
                }
            }


            var temp = "";
            for (int i = 0; i < len; i++)
            {
                temp += X[i];
            }
            var result = Convert.ToInt32(temp);
            return result;
        }
    }

 

posted on 2018-10-09 20:40  Sempron2800+  阅读(161)  评论(0编辑  收藏  举报