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

这道题给了一个非负数,让我们求一个数字小于等于给定数字,且该数字各位上的数字是单调递增的。先来分析题目中给的几个例子吧,首先如果是 10 的话,由于1大于0,所以不是单调自增的,那么返回的数就是9。第二个例子是 1234,各位上已经满足单调自增的条件了,返回原数即可。第三个例子是 332,最后一位2小于之前的3,那么此时将前面位减1,先变成322,再往前看,还是小于前面的3,那么再将前面位减1,就变成了 222,此时 222 不是最大的单调递增数,可以将后面两位变成9,于是乎就有了 299,小于给定的 332,符合题意。如果给定的数字是 232,那么就会得到 229,这样可以发现规律,要找到从后往前遍历的最后一个值升高的位置,让前一位减1,并把当前位以及后面的所有位都变成9,就可以得到最大的单调递增数啦。

用j表示最后一个值升高的位置,具体来说应该是其前一位的值大,初始化为总位数n,然后从后往前遍历,因为每次要和前一位比较,为防止越界,应遍历到第二个数停止,如果当前位大于等于前一位,符合单调递增,直接跳过;否则就将前一位自减1,j赋值为当前位i,循环结束后,从j位到末尾的位数都改为9即可,参见代码如下:

class Solution {
public:
int monotoneIncreasingDigits(int N) {
string str = to_string(N);
int n = str.size(), j = n;
for (int i = n - ; i > ; --i) {
if (str[i] >= str[i - ]) continue;
--str[i - ];
j = i;
}
for (int i = j; i < n; ++i) {
str[i] = '';
}
return stoi(str);
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/738

类似题目:

Remove K Digits

参考资料:

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

https://leetcode.com/problems/monotone-increasing-digits/discuss/109811/Simple-and-very-short-C%2B%2B-solution

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 738. Monotone Increasing Digits 单调递增数字的更多相关文章

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

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

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

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

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

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

  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. 记录几篇PM文章

    今日阅读了几篇关于 PM 的文章感觉还不错,整理.摘录于此: 1.[项目经理和产品经理:https://www.cnblogs.com/joylee/p/3541141.html] ——关于二者的异同 ...

  2. python正则图片爬取

    # conding:utf8 import requests import re import time if __name__ == "__main__": # 所有的数据 ur ...

  3. Vue.js 源码分析(二十二) 指令篇 v-model指令详解

    Vue.js提供了v-model指令用于双向数据绑定,比如在输入框上使用时,输入的内容会事实映射到绑定的数据上,绑定的数据又可以显示在页面里,数据显示的过程是自动完成的. v-model本质上不过是语 ...

  4. dbvisualizer客户端执行创建存储过程或自定义函数语句的方法

    DBVisualizer这个数据库客户端工具,如果要执行存储过程或函数的话,需要在创建存储过程或函数的语句的最前面和末尾分别加上[--/]和[/]符号. --/ CREATE FUNCTION B22 ...

  5. Kubernetes PV与PVC的关系

    Kubernetes PV与PVC的关系 PersistenVolume(PV):对存储资源创建和使用的抽象,使得存储作为集群中的资源管理,分为有静态与动态.PersistentVolumeClaim ...

  6. Python 线程池(小节)

    Python 线程池(小节) from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor import os,time, ...

  7. SqlServer 创建数据库两种方式

    一个SqlServer 数据库实例大概可以创建三万多个数据库. 创建数据库的第一种方式:SqlServer Management Studio管理工具进行可视化创建. 1).打开数据库管理工具,在&q ...

  8. ASP.NET MVC 实现简单的登录

    1.创建一个控制器   如下: using System; using System.Collections.Generic; using System.Linq; using System.Web; ...

  9. JDK1.8 Stream

    Java 8 API添加了一个新的抽象称为流Stream,可以让你以一种声明的方式处理数据. Stream 使用一种类似用 SQL 语句从数据库查询数据的直观方式来提供一种对 Java 集合运算和表达 ...

  10. js字符串转为数字方法parseInt()、减号、乘号、JSON.parse()、Number()的效率比较

    var a = '1'; var b = '0x1'; var runTest = function(timeTag, testFunction) { console.time(timeTag); f ...