[抄题]:

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

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 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 单调递增的最接近数字的更多相关文章

  1. [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 ...

  2. [LeetCode] Monotone Increasing Digits 单调递增数字

    Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...

  3. 【LeetCode】738. Monotone Increasing Digits 解题报告(Python)

    [LeetCode]738. Monotone Increasing Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...

  4. 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 ...

  5. 738. Monotone Increasing Digits

    Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...

  6. 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 ...

  7. [Swift]LeetCode738. 单调递增的数字 | Monotone Increasing Digits

    Given a non-negative integer N, find the largest number that is less than or equal to Nwith monotone ...

  8. 【leetcode】Monotone Increasing Digits

    Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...

  9. [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 ...

随机推荐

  1. .htaccess FollowSymlinks影响rewrite功能

    Thinkphp的框架的根目录的.htaccess是这样写的: <IfModule mod_rewrite.c> Options +FollowSymlinks RewriteEngine ...

  2. jQuery 点击后退(返回)执行函数

    <html> <head> <meta charset="UTF-8"> <meta name="viewport" ...

  3. 20175202 《Java程序设计》第四周学习总结

    20175202 <Java程序设计>第四周学习总结 第五章学习内容 1.子类的继承性: (1)子类与父类在同一包中的继承性:子类自然地继承了其父类中不是private的成员变量作为自己的 ...

  4. 内置---排序(sorted)

    # li = [1,23,4,5,6,6,7]# res = sorted(li,reverse=True) #反转后,从小到大 默认从大到小 #res = sorted(li) # print(re ...

  5. css 样式控制文本过长实现省略号

    css 样式控制文本过长实现省略号 .topicTitle{ text-overflow: ellipsis; max-width: 75%; overflow: hidden; white-spac ...

  6. Android手机上Audio DSP频率低 memory小的应对措施

    我在前面的文章(Android智能手机上的音频浅析)中说过Android手机上有一块专门用于音频处理的DSP,它的特点是频率低(一般几百MHZ).内部memory小(通常不超过100k word).要 ...

  7. IntelliJ IDEA中Terminal路径的问题(win7环境)

    在安装java jdk,配置系统变量后,再安装idea,有时候会出现使用idea中Termimal进行编译运行java文件出现,javac/java不是内部命令,或者“错误: 找不到或无法加载主类”的 ...

  8. Python第一天:python2.x和python3.x的区别

    查看Python版本 # python -V Python2.7.5是centos7中默认安装的Python [root@localhost ~]# python -V Python [root@lo ...

  9. windwos下基于exp的提权

    00x1: 本文是基于windwos 补丁的漏洞提权. 其实是一件很简单的工作:如果看成数据库的3张表分别是: 未打补丁表, 补丁漏洞表, 漏洞利用提权程序表. 为一一对应关系,所以就是资源的掌握,以 ...

  10. MyISAM与InnoDB两者之间区别与选择,详细总结,性能对比

    1.MyISAM:默认表类型,它是基于传统的ISAM类型,ISAM是Indexed Sequential Access Method (有索引的顺序访问方法) 的缩写,它是存储记录和文件的标准方法.不 ...