[抄题]:

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. Announcing the Operate Preview Release: Monitoring and Managing Cross-Microservice Workflows

    转自:https://zeebe.io/blog/2019/04/announcing-operate-visibility-and-problem-solving/   Written by Mik ...

  2. .htaccess FollowSymlinks影响rewrite功能

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

  3. 3.3 MathType自动公式编号和对齐

    1.利用MathType录入公式并自动公式编号.自动对齐. 将光标置于需录入公式位置,点[MathType]>[(1)Insert Number]>下拉选择[1.a Format]. 如下 ...

  4. 十八、springcloud(四)熔断器

    1.熔断器(Hystrix) a.断路器机制 断路器很好理解, 当Hystrix Command请求后端服务失败数量超过一定比例(默认50%), 断路器会切换到开路状态(Open). 这时所有请求会直 ...

  5. nginx 代理flask应用的uwsgi配置

    socket代理配置: 关于uwsgi的用法,请自行百度,这里只针对socket文件和端口的不同,进行单一的记录. 这种方式启动的flask应用,由于是通过socket与nginx通信的,所以必须制定 ...

  6. 二进制 转换成十进制 BCD码(加3移位法)

    "原来的二进制数十几位,则左移时就要左移几位" "二进制数调整BCD码的方法是将二进制码左移8次,每次移位后都检查低四位LSD+3是否大于7,如是则加3,否则不加,高4位 ...

  7. 将Long类型转为字母数字组合的jar包---Hashids

    在设计数据库时,我有时喜欢使用自增Id,而不是uuid,但是在面对终端用户时,直接暴露id不是一个好的行为. 经过查询,可以使用 Hashids 这个jar包将id转为类似YouTube的大小写字母和 ...

  8. 涂抹mysql笔记-管理mysql库和表

    mysql的表对象是基于库维护的,也就是说它属于某个库,不管对象是由谁创建的,只要库在表就在.这根Oracle不同Oracle中的表对象是基于用户的.属于创建改对象的用户所有,用户在表就在.mysql ...

  9. jdbc链接数据库的url两种写法

    首先看下面两个jdbc的url 一:jdbc.url=jdbc:oracle:thin:@100.2.194.200:1521/abc二:jdbc.url=jdbc:oracle:thin:@100. ...

  10. 第17课 类型萃取(1)_基本的type_traits

    1. type_traits类型萃取 (1)type_traits通过定义一些结构体或类,并利用模板类特化和偏特化的能力,给类型赋予一些特性,这些特性根据类型的不同而异.在程序设计中可以使用这些tra ...