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 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的更多相关文章
- 【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 ...
随机推荐
- python3学习特性
一 实例变量与类变量 class Pepple: __age=18 __name="zhangfff" @classmethod def GetInfo(cls): print(c ...
- JPA中的复杂查询
JPQL全称Java Persistence Query Language 基于首次在EJB2.0中引入的EJB查询语言(EJB QL),Java持久化查询语言(JPQL)是一种可移植的查询语言,旨在 ...
- SQL使用 dateadd添加使天数加x
,Receivedate)), ) --第一个参数 表示增加什么(day ) --第二个参数表示增加多少( int ) --第三个参数表示那个字段 (Receivedate 字段属性) conver ...
- Hadoop_28_MapReduce_自定义 inputFormat
1. 自定义inputFormat 1.1.需求: 无论hdfs还是mapreduce,对于小文件都有损效率,实践中,又难免面临处理大量小文件,此时就需要有相应解决方案; 1.2.分析: 小文件的优化 ...
- R中数据的输入和数据的标注
数据的导入 默认情况下数据导入时,字符型变量将转化为因子.若不希望转化,可设置 stringsAsFactors=FALSE 从带分隔符的文本文件中导入数据 read.table() file --& ...
- 数据结构与算法——常用排序算法及其Java实现
冒泡排序 原理:依次比较相邻的两个数,将小数放在前面(左边),大数放在后面(右边),就像冒泡一样具体操作:第一趟,首先比较第1个和第2个数,将小数放前,大数放后.然后比较第2个数和第3个数,将小数放前 ...
- 深入了解java线程池(转载)
出处:http://www.cnblogs.com/dolphin0520/ 本文归作者海子和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责 ...
- 错误调试以及debug的使用
/*定义 .search 搜索*/ $.fn.UiSearch=function(){ var ui=$(this); //任何地方都可以使用断点调试:debugger; //调试时,可以在控制台输入 ...
- linux 下使用github
Linux下Git和GitHub环境的搭建 1.创建Github帐号 (name@server.com) 2.安装git [root@cloud ~]# yum install git -y 3.生 ...
- Java进阶知识13 Hibernate查询语言(HQL),本文以hibernate注解版为例讲解
1.简单概述 1.1. 1) SQL:面向的是数据库 select * from tableName;2) HQL查询(Hibernate Query language): hibernate 提供的 ...