zoukankan      html  css  js  c++  java
  • 738. Monotone Increasing Digits

    /**
     * 738. Monotone Increasing Digits
     * https://leetcode.com/problems/monotone-increasing-digits/description/
     *
     * Given a non-negative integer N, find the largest number that is less than or equal to N with monotone increasing digits.
    (Recall that an integer has monotone increasing digits if and only if each pair of adjacent digits x and y satisfy x <= y.)
    Example 1:
    Input: N = 10
    Output: 9
    Example 2:
    Input: N = 1234
    Output: 1234
    Example 3:
    Input: N = 332
    Output: 299
     * */
    class Solution {
        fun monotoneIncreasingDigits(N: Int): Int {
            val nArray = N.toString().toCharArray()
            val size = nArray.size
            var j = size//用于记录要转换的位置
            for (i in size - 1 downTo 1) {
                if (nArray[i] >= nArray[i - 1]) {
                    continue
                }
                //要找到从后往前遍历的最后一个值升高的位置,让前一位减1
                nArray[i - 1]--
                j = i
            }
            //并把当前位以及后面的所有位都变成9
            for (i in j until size) {
                nArray[i] = '9'
            }
            val sb = StringBuilder()
            for (item in nArray) {
                sb.append(item)
            }
            return sb.toString().toInt()
        }
    }
  • 相关阅读:
    Thymeleaf标签使用
    mybatis映射和条件查询
    开发模型
    Sentinel降级服务
    Sentinel
    Nacos注册中心
    SpringCloudAlibaba简介
    Sleuth
    Stream消息驱动
    如何用JAVA爬取AJAX加载后的页面(利用phantomjs)【以天眼查为例】
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/12245367.html
Copyright © 2011-2022 走看看