[LeetCode] 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
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
类似题目:
参考资料:
https://leetcode.com/problems/monotone-increasing-digits/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 738. Monotone Increasing Digits 单调递增数字的更多相关文章
- [LeetCode] Monotone Increasing Digits 单调递增数字
Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...
- 738. Monotone Increasing Digits 单调递增的最接近数字
[抄题]: Given a non-negative integer N, find the largest number that is less than or equal to N with m ...
- 【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 ...
- 【leetcode】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-738-Monotone Increasing Digits]
Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...
随机推荐
- centos7 更换为aliyun的yum源
rm -f /etc/yum.repos.d/* wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Ce ...
- vue 移动端上传图片结合localResizeIMG插件进行图片压缩
localResizeIMG插件的功能是将图片进行压缩,然后转换成base64传给后台. 首先, npm i lrz -save 然后,再main.js里面引入lrz import lrz from ...
- 发布TS类型文件到npm
最近发布了@types/node-observer包到npm,这里记录下发布过程 TS类型文件的包名通常以@types开头,使用npm publish发布以@types开头的包时需要使用付费账号. ...
- DVWA-文件上传学习笔记
DVWA-文件上传学习笔记 一.文件上传漏洞 文件上传漏洞,通常是由于对上传文件的类型.内容没有进行严格的过滤.检查,导致攻击者恶意上传木马以便获得服务器的webshell权限. 二.DVWA学习 将 ...
- @Valid 数据校验 + 自定义全局异常信息
关于javax.validation.Validator校验的使用 对于要校验的实体类:其需要校验的字段上需要添加注解 实际例子 使用:首先要拿到 validator的子类 Validator val ...
- C# 实现 奇数偶数排序,奇数在前,偶数在后
public static void SortByOddAndEven(int []arr) { for (int i = 0; i < arr.Length; i++) { for (int ...
- asp.net 创建虚拟目录 iis创建虚拟目录
这几天本人接了个档案管理查询系统的小项目,踩过的坑. 其实功能都挺简单的,大致要求客户有很多pdf文档,为了方便管理,所有要开发一个相当于文件管理系统,本人正好有现成的文件管理系统,修改下就可以.其中 ...
- Java中配置文件的三种配置位置及读取方式
XML 和properties properties: 1.存放于src根目录下 //获取到同包下的资源文件,将其转换成流对象 //InputStream is= PropertiesDemo.cla ...
- SQLAlchemy--基本增删改查
目录 简介 安装 组成部分 简单使用 执行原生sql(不常用) orm使用(重点) 常用数据类型 Column常用参数 常用操作(CURD) 创建映射类的实例 创建会话Session 增加add()/ ...
- Linux性能调优 | 01 平均负载的理解和分析
01 uptime命令 通常我们发现系统变慢时,我们都会执行top或者uptime命令,来查看当前系统的负载情况,比如像下面,我执行了uptime,系统返回的了结果. [root@lincoding ...