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
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩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 ...
随机推荐
- [c#]_ELVE_Message多功能用法
1. 当要显示如图3个按钮时,并要获得单击不同按钮的进行不同的相应时,可以在MessageBoxButtons后面添加一个.(应该英文的点,此处为了醒目,用中文代替)可以看到提示框下方需要几个按 ...
- 重读 谢希仁《计算机网络》3 - 网络层和IP协议
- 创建一个dynamics 365 CRM online plugin (三) - PostOperation
上两节我们创建了一个 PreOperation的plugin 今天我们创建一个PostOpeartion的plugin和之前的plugin连接起来 当创建contact之后,我们要添加一个task给新 ...
- MVC 中Scripts.Render、Styles.Render
在ASP.NET MVC项目中,可以在视图中利用Scripts.Render.Styles.Render统一加载js.css文件,需要利用BundleConfig类来Add 各种Bundle,例如:b ...
- jenkins使用(ubuntu16.0环境)
本文总结了使用jenkins过程.大部分是网上链接,以后自已查看使用. ssh远程链接服务器 检查是否开启ssh ps -ef|grep ssh 1.安装ssh 2.开启root用户 3.充许ro ...
- VUE 微信开发
1.工具 1.电脑版微信客户端window版本(1.x.x 亲测可以在谷歌浏览器进行微信授权登录,版本越来越好)或者用微信开发工具.很久之前就是用这个方法搞定用chrome进行微信登录授权. 2.us ...
- 阿里云轻量应用服务器 怎么控制怎么上传文件怎么安装JDK和Tomcat怎么完成JavaWeb的部署
你是否遇到过这些问题,自己的javaweb项目本地运行一切正常,但是一旦转移到阿里服务器之类的.就出现以下问题. 1 jsp无法解析java类 2 Only a type can be importe ...
- nginx gzip配置
参考: https://docs.nginx.com/nginx/admin-guide/web-server/compression/ server { gzip on; gzip_types ...
- git比较两个分支的文件的差异
Git diff branch1 branch2 --stat //显示出所有有差异的文件列表 Git diff branch1 branch2 文件名(带路径) //显示指定文件的详细差异 ...
- CSS之display
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...