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:

  1. Input: N = 10
  2. Output: 9

Example 2:

  1. Input: N = 1234
  2. Output: 1234

Example 3:

  1. Input: N = 332
  2. 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.

  1. class Solution {
  2. public static int monotoneIncreasingDigits(int N) {
  3. char[] Nstr = String.valueOf(N).toCharArray();
  4. int cnt = 0;
  5. boolean firstmeet = true;
  6. while(cnt < Nstr.length-1){
  7. if(Nstr[cnt] > Nstr[cnt+1]){
  8. if(firstmeet) {
  9. for(int i=cnt+1; i<Nstr.length; i++) Nstr[i] = '9';
  10. Nstr[cnt] = (char)((int)Nstr[cnt] - 1);
  11. firstmeet = false;
  12. }else{
  13. Nstr[cnt+1] = '9';
  14. }
  15. if(cnt > 0) {
  16. cnt-=2;
  17. firstmeet = true;
  18. }
  19. }
  20. cnt++;
  21. }
  22. return Integer.parseInt(String.valueOf(Nstr));
  23. }
  24. }

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. ASE19团队项目alpha阶段model组 scrum3 记录

    本次会议于11月5日,19时整在微软北京西二号楼sky garden召开,持续25分钟. 与会人员:Jiyan He, Kun Yan, Lei Chai, Linfeng Qi, Xueqing W ...

  2. python多继承下的查找顺序-MRO原则演变与C3算法

    在python历史版本中的演变史 python2.2之前: MRO原则: 只有经典类,遵循深度优先(从左到右)原则, 存在的问题:在有重叠的多继承中,违背重写可用原则 解决办法是再设计类的时候不要设计 ...

  3. RT-Thread代码启动过程与$Sub$ $main、$Super$ $main

    文章转载自:https://blog.csdn.net/yang1111111112/article/details/80913001 我们找到系统复位的地方,可以往下单步跟踪. ①从系统初始化开始执 ...

  4. Hadoop_22_MapReduce map端join实现方式解决数据倾斜(DistributedCache)

    1.Map端Join解决数据倾斜   1.Mapreduce中会将map输出的kv对,按照相同key分组(调用getPartition),然后分发给不同的reducetask 2.Map输出结果的时候 ...

  5. 工作中常用的Git操作

    粘贴自:微信公众号:程序员共成长 分支操作: git branch 创建分支 git branch -b 创建并切换到新建的分支上 git checkout 切换分支 git branch 查看分支列 ...

  6. delphi Tidhttp 发送json格式报文

    type TwmsThreadpostJson = class(TThread) private Furl: string; Fpostcmd: string; FResult: string; FB ...

  7. 《黑白团团》第九次团队作业:Beta冲刺与验收准备

    项目 内容 作业课程地址 任课教师首页链接 作业要求 团队项目 填写团队名称 黑白团团队 填写具体目标 认真负责,完成项目 GitHub链接 Scrum meeting导航 [Beta]Scrum m ...

  8. 内核对象&句柄&泄漏&检测

    今天看到这个问题如何评价王垠的 <讨厌的 C# IDisposable 接口>? - 王垠(人物),答案被歪到windows 内核对象和句柄,答案中谈的太浅显而且有误.翻出陈年老文章(此文 ...

  9. LVM卷管理

    一.LVM是做什么的 LVM ( Logical Volume Manager ,逻辑卷管理器)LVM 是建立在磁盘和分区之上的一个逻辑层,用来提高磁盘分区管理的灵活性.LVM 可以对磁盘分区按照组的 ...

  10. 1.安装Loucust

    python 3.x 通过pip安装: pip install locustio 如果是以分布式队列运行locust,需要装一种通信队列的库pyzmq  :pip install pyzmq 安装成功 ...