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 ysatisfy x <= y.)

Example 1:

Input: N = 10
Output: 9

Example 2:

Input: N = 1234
Output: 1234

Example 3:

Input: N = 332
Output: 299

Note: N is an integer in the range [0, 10^9].

Runtime: 16 ms, faster than 87.76% of Java online submissions for Monotone Increasing Digits.

class Solution {
public static int monotoneIncreasingDigits(int N) {
char[] Nstr = String.valueOf(N).toCharArray();
int cnt = 0;
boolean firstmeet = true;
while(cnt < Nstr.length-1){
if(Nstr[cnt] > Nstr[cnt+1]){
if(firstmeet) {
for(int i=cnt+1; i<Nstr.length; i++) Nstr[i] = '9';
Nstr[cnt] = (char)((int)Nstr[cnt] - 1);
firstmeet = false;
}else{
Nstr[cnt+1] = '9';
}
if(cnt > 0) {
cnt-=2;
firstmeet = true;
}
}
cnt++;
}
return Integer.parseInt(String.valueOf(Nstr));
}
}

LC 738. Monotone Increasing Digits的更多相关文章

  1. 【LeetCode】738. Monotone Increasing Digits 解题报告(Python)

    [LeetCode]738. Monotone Increasing Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...

  2. 738. Monotone Increasing Digits 单调递增的最接近数字

    [抄题]: Given a non-negative integer N, find the largest number that is less than or equal to N with m ...

  3. 402. Remove K Digits/738.Monotone Increasing Digits/321. Create Maximum Number

    Given a non-negative integer num represented as a string, remove k digits from the number so that th ...

  4. [LeetCode] 738. Monotone Increasing Digits 单调递增数字

    Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...

  5. 738. Monotone Increasing Digits

    Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...

  6. [LeetCode] Monotone Increasing Digits 单调递增数字

    Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...

  7. [Swift]LeetCode738. 单调递增的数字 | Monotone Increasing Digits

    Given a non-negative integer N, find the largest number that is less than or equal to Nwith monotone ...

  8. 【leetcode】Monotone Increasing Digits

    Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...

  9. [leetcode-738-Monotone Increasing Digits]

    Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...

随机推荐

  1. mysql启动失败,unit not found

    1 mysql启动 Failed to start mysqld.service: Unit not found.   2 查询/etc/init.d/下是否存在mysqld ll /etc/init ...

  2. 玩转springcloud(二):注册中心-Eureka

    一.简介 注册中心 注册中心是服务发现的核心.它保存了各个可用服务实例的网络地址(IP Address和Port).服务注册中心必须要有高可用性和实时更新功能. Netflix Eureka 就是一个 ...

  3. C# Winform 禁止一个进程运行多次

    禁止一个进程运行多次 using System; using System.Windows.Forms; namespace StartExe { static class Program { /// ...

  4. 自己手写实现Dubbo

    目录 dubbo 简单介绍 为什么手写实现一下bubbo? 什么是RPC? 接口抽象 服务端实现 注册中心 消费者端: dubbo 简单介绍 dubbo 是阿里巴巴开源的一款分布式rpc框架. 为什么 ...

  5. 基本算法 st

    今天困得不行,就看了个小算法st,其实和线段树的作用一样, 不过这个算法没有用到数据结构,使用二进制优化的 是O(log(n)n)的时间预处理,然后以O(1)的时间返回(l,r)上的最大或最小 #in ...

  6. 【django】另一种思路代替nginx 的rewrite

    需求:访问xx.com 跳转到xx.com/index 修改setting 同级别的urls.py 文件 from django.conf.urls import include, url from ...

  7. Muduo阅读

    创建了EventLoop对象的线程是IO线程,其主要功能是运行事件循环EventLoop::loop(), one loop per thread 事件循环必须在IO线程中运行 Reactor关键结构 ...

  8. 回顾了下shell 编程的一些细节

    回顾了一些细节 shell 的通配符扩展   *  ? []  [^xxxx]  {aaa,bbb}         子进程 more `grep -l POSIX *` more $(grep -l ...

  9. unity shader 剔除指定的颜色

    Shader "MyShader/PaintingBGTransparency" { Properties{ _MainTex("Base (RGB)", 2D ...

  10. javascript内置对象一:String

    <script>    //string.split("-"),以字符"-"把string拆分成一个数组.返回一个数组    //array.joi ...