LC 738. Monotone Increasing Digits
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
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的更多相关文章
- 【LeetCode】738. Monotone Increasing Digits 解题报告(Python)
[LeetCode]738. Monotone Increasing Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...
- 738. Monotone Increasing Digits 单调递增的最接近数字
[抄题]: Given a non-negative integer N, find the largest number that is less than or equal to N with m ...
- 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 ...
- [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 ...
- 738. Monotone Increasing Digits
Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...
- [LeetCode] Monotone Increasing Digits 单调递增数字
Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...
- [Swift]LeetCode738. 单调递增的数字 | Monotone Increasing Digits
Given a non-negative integer N, find the largest number that is less than or equal to Nwith monotone ...
- 【leetcode】Monotone Increasing Digits
Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...
- [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 ...
随机推荐
- ASE19团队项目alpha阶段model组 scrum3 记录
本次会议于11月5日,19时整在微软北京西二号楼sky garden召开,持续25分钟. 与会人员:Jiyan He, Kun Yan, Lei Chai, Linfeng Qi, Xueqing W ...
- python多继承下的查找顺序-MRO原则演变与C3算法
在python历史版本中的演变史 python2.2之前: MRO原则: 只有经典类,遵循深度优先(从左到右)原则, 存在的问题:在有重叠的多继承中,违背重写可用原则 解决办法是再设计类的时候不要设计 ...
- RT-Thread代码启动过程与$Sub$ $main、$Super$ $main
文章转载自:https://blog.csdn.net/yang1111111112/article/details/80913001 我们找到系统复位的地方,可以往下单步跟踪. ①从系统初始化开始执 ...
- Hadoop_22_MapReduce map端join实现方式解决数据倾斜(DistributedCache)
1.Map端Join解决数据倾斜 1.Mapreduce中会将map输出的kv对,按照相同key分组(调用getPartition),然后分发给不同的reducetask 2.Map输出结果的时候 ...
- 工作中常用的Git操作
粘贴自:微信公众号:程序员共成长 分支操作: git branch 创建分支 git branch -b 创建并切换到新建的分支上 git checkout 切换分支 git branch 查看分支列 ...
- delphi Tidhttp 发送json格式报文
type TwmsThreadpostJson = class(TThread) private Furl: string; Fpostcmd: string; FResult: string; FB ...
- 《黑白团团》第九次团队作业:Beta冲刺与验收准备
项目 内容 作业课程地址 任课教师首页链接 作业要求 团队项目 填写团队名称 黑白团团队 填写具体目标 认真负责,完成项目 GitHub链接 Scrum meeting导航 [Beta]Scrum m ...
- 内核对象&句柄&泄漏&检测
今天看到这个问题如何评价王垠的 <讨厌的 C# IDisposable 接口>? - 王垠(人物),答案被歪到windows 内核对象和句柄,答案中谈的太浅显而且有误.翻出陈年老文章(此文 ...
- LVM卷管理
一.LVM是做什么的 LVM ( Logical Volume Manager ,逻辑卷管理器)LVM 是建立在磁盘和分区之上的一个逻辑层,用来提高磁盘分区管理的灵活性.LVM 可以对磁盘分区按照组的 ...
- 1.安装Loucust
python 3.x 通过pip安装: pip install locustio 如果是以分布式队列运行locust,需要装一种通信队列的库pyzmq :pip install pyzmq 安装成功 ...