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 ...
随机推荐
- WaitType:ASYNC
项目组有一个数据库备份的Job运行异常,该Job将备份数据存储到remote server上,平时5个小时就能完成的备份操作,现在运行19个小时还没有完成,backup命令的Wait type是 AS ...
- sqlserver 2008修改数据库表的时候错误提示“阻止保存要求重新创建表的更改”
当用户在在SQL Server 2008企业管理器中更改表结构时,必须要先删除原来的表,然后重新创建新表,才能完成表的更改,如果强行更改会出现以下提示:不允许保存更改.您所做的更改要求删除并重新创建以 ...
- XXX_initcall()函数分析
1. 先看这些宏的定义(定义在文件include/linux/init.h中) #define pure_initcall(fn) __define_initcall("0",fn ...
- 前端基础(五):jQuery
jQuery介绍 jQuery是一个轻量级的.兼容多浏览器的JavaScript库. jQuery使用户能够更方便地处理HTML Document.Events.实现动画效果.方便地进行Ajax交互, ...
- cifs
加入cifs 能把设备的目录挂载到我们的电脑上面 mount -t cifs -o username=Everyone //192.168.23.72/cifs /cif 如果当前是everyone ...
- C语言创建线程以及使用锁进行读写分离
线程的使用 1.线程的创建 线程的相关操作放在<pthread.h>中. 1.1我们定义一个线程,首先要进行定义一个函数,类似我们创建一个a线程 void *thread_a(void * ...
- 剖析ajax
学过javascript和接触过后端PHP语言必然要用到ajax,这是必学的一门学科,AJAX指的是Asynchronous JavaScript and XML,它使用XMLHttpRequest对 ...
- java——从.net再学习java
到底从java中学到了什么? 1,java是由sun公司发明的,sun希望制定一些标准,具体的实现交给具体的厂商来自己实现: 2,java是开源的,第三方做了很多自己的一些组件实现,比如: 很多时候, ...
- CF 976F 递增容量最大流
给你一个二分图 要求你求出对于k=[0~Mindegree] 每个点的度数至少为k所需要的最少边数 并输出方案 如果是单个询问的话 直接跑一个下界网络流即可 但是有多个询问 重建图强行跑不行 反过来考 ...
- python : import详解。
用户输入input() input()函数: 用于从标准输入读取数值. >>> message = input('tell me :') tell me :hahah >> ...