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

Note: N is an integer in the range [0, 10^9].

思路:

The idea is to go from the LSB to MSB and find the last digit, where an inversion happens.
There are 2 cases to consider:

case 1:
In 14267 , we see that inversion happens at 4. In this case, then answer is obtained by reducing 4 to 3, and changing all the following digits to 9.
=> 13999

case 2:
1444267, here eventhough the last inversion happens at the last 4 in 1444, if we reduce it to 3, then that itself breaks the rule. So once we find the last digit where inversion happens, if that digit is repeated, then we have to find the last position of that digit. After that it is same as case1, where we reduce it by 1 and set the remaining digits to 9.
=> 1399999

The steps are:

    1. Convert n into num array in reverse order
    2. Find the leftmost position that is inverted and if the inverted character repeats itself, find the leftmost repeated digit.
    3. Fill the digits after inversion as 9
    4. Reduce the digit that caused the inversion by -1
    5. Reverse back the num array and convert to int
int monotoneIncreasingDigits(int N) {
string n_str = to_string(N); int marker = n_str.size();
for(int i = n_str.size()-; i > ; i --) {
if(n_str[i] < n_str[i-]) {
marker = i;
n_str[i-] = n_str[i-]-;
}
} for(int i = marker; i < n_str.size(); i ++) n_str[i] = ''; return stoi(n_str);
}

参考:

https://leetcode.com/problems/monotone-increasing-digits/discuss/109811

https://leetcode.com/problems/monotone-increasing-digits/solution/

https://leetcode.com/problems/monotone-increasing-digits/discuss/109794

[leetcode-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】738. Monotone Increasing Digits 解题报告(Python)

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

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

  4. 738. Monotone Increasing Digits 单调递增的最接近数字

    [抄题]: Given a non-negative integer N, find the largest number that is less than or equal to N with m ...

  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. 【leetcode】Monotone Increasing Digits

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

  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. [Swift]LeetCode738. 单调递增的数字 | Monotone Increasing Digits

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

随机推荐

  1. 前端ajax的各种与后端交互的姿势

    前端中常常用的与后端交换数据的话,通常是要用到ajax这种方法的 但是交互的方式有很多种,很多取决于你后端的属性,我这儿主要列举我目前项目比较常用的两种方式 --一个是我们通用的web api和控制器 ...

  2. Qt5连接Mysql环境配置

    已安装的环境:Mysql5.7 64bit ,Qt5.12 64bit. 到mysql官方下载mysql5.7 64bit的压缩包,解压,复制下图框内四个文件. 将四个文件复制到Qt安装目录下bin目 ...

  3. input 输入的一些限制说明

    input输入框 只能输入 数字可以有小数点 <input class="form_text" id="purchasePrice" name=" ...

  4. Spark运行模式_基于YARN的Resource Manager的Client模式(集群)

    现在越来越多的场景,都是Spark跑在Hadoop集群中,所以为了做到资源能够均衡调度,会使用YARN来做为Spark的Cluster Manager,来为Spark的应用程序分配资源. 在执行Spa ...

  5. 【NXP开发板应用—智能插排】4. PWM驱动

    [前言] 首先感谢深圳市米尔科技有限公司举办的这次活动并予以本人参加这次活动的机会,以往接触过嵌入式,但那都是皮毛,最多刷个系统之类的,可以说对于嵌入式系统开发这件事情是相当非常陌生的,这次活动为我提 ...

  6. 20190112-自定义实现字符串的操作方法,如strip,upper,title,ljust,center,zfill,find,rfind等

    1:自定义实现strip()Python strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列算法:strip()仅移除首尾的指定字符,不能移除中间的先从首部开始移除 de ...

  7. python七类之整型布尔值

    整型与布尔值 一.关键字:整型 --->int     布尔值----->bool  : True  真  False  假 1.整形和布尔值都是不可变得不可迭代的数据类型 2.整型: 主 ...

  8. U盘kali系统安装

      正言: 起初先百度了一下U盘安装Kali的资料,有很多版本和方法,当然还是以百度经验为例开始操作https://jingyan.baidu.com/article/cdddd41ca1027e53 ...

  9. 检测微信小程序是否被反编译获取源码

    众所周知,微信小程序的代码安全性很弱,很容易被别人反编译获取源码.我自己的小程序也被别人反编译拿到源码还上线了,非常无语. 既然客户端不好防范,服务端还是可以做点手脚的. 小程序的Referer是不可 ...

  10. tkinter的入门,估计也只能站门口

    from tkinter import * import tkinter.messagebox as messagebox #创建一个继承frame的类,是所有小部件(widget的容器) #widg ...