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
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
有i - 1的时候不能退到0,最多退到1
[思维问题]:
完全没思路啊
[英文数据结构或算法,为什么不用别的数据结构或算法]:
数字转字符串要用String.valueOf(N),而不是(string)强制转换
[一句话思路]:
前一位数比较大就先标记,后面都改成9
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- mark需要初始化为最后一位数
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
大不了前面位上的数-1,后面都是9
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
public int monotoneIncreasingDigits(int N) {
//corner case
if (N <= 9) return N;
//initialization: digits, mark
char[] digits = String.valueOf(N).toCharArray();
int mark = digits.length - 1;
//for loop and get the bigger num from i - 1
for (int i = digits.length - 1; i > 0; i--) {
if (digits[i] < digits[i - 1]) {
mark = i - 1;
digits[i - 1]--;
}
}
//change the later nums into 9
for (int j = mark + 1; j < digits.length; j++) {
digits[j] = '9';
}
//return
return Integer.parseInt(new String(digits));
}
}
738. Monotone Increasing Digits 单调递增的最接近数字的更多相关文章
- [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 ...
- [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 解题报告(Python)
[LeetCode]738. Monotone Increasing Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...
- 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 ...
- 738. Monotone Increasing Digits
Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...
- LC 738. 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 ...
随机推荐
- MyBatis举例以及连接数据库过程
第一:创建实体类 package entity; public class Emp { private int empno; private String ename; private String ...
- flask,gunicorn,supervisor,nginx配置服务器接口
1,申请阿里云主机 2,apt-get update 3,apt-get install pip 4,pip install virtualenv 5,virtualenv venv 6,source ...
- shiro(安全框架)
shiro.apache.org JavaSE环境搭建Shiro框架 1/导入与 shiro相关的Jar包 所有集好的环境可以在如下目录查找 复制如上文件到工程中 2/配置文件:储存临时文件 shir ...
- Dart Map<> 添加 元素
Map<String, WidgetBuilder> routesList() { Map<String, WidgetBuilder> re = new Map<Str ...
- <亲测>centos7通过yum安装JDK1.8(实际上是openjdk)
centos7通过yum安装JDK1.8 安装之前先检查一下系统有没有自带open-jdk 命令: rpm -qa |grep java rpm -qa |grep jdk rpm -qa |gr ...
- Android手机上Audio DSP频率低 memory小的应对措施
我在前面的文章(Android智能手机上的音频浅析)中说过Android手机上有一块专门用于音频处理的DSP,它的特点是频率低(一般几百MHZ).内部memory小(通常不超过100k word).要 ...
- [TFS教程]TFS: Get Command
Get Command Visual Studio 2012 Gets (downloads) either the latest version or a specified version o ...
- java web 三大核心组件Filter
Filter 过滤: 定义: 过滤器是客户端与服务端之间的一道网,可以对请求的数据进行拦截和修改,同时也可以对,响应数据进行拦截和修改
- JVM内部细节之一:synchronized关键字及实现细节(轻量级锁Lightweight Locking)
在C程序代码中我们可以利用操作系统提供的互斥锁来实现同步块的互斥访问及线程的阻塞及唤醒等工作.然而在Java中除了提供Lock API外还在语法层面上提供了synchronized关键字来实现互斥同步 ...
- Promise事件比timeout优先
Promise, setTimeout 和 Event Loop 下面的代码段,为什么输出结果是1,2,3,5,4而非1,2,3,4,5?(function test() { setTimeout(f ...