[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].
思路:
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:
- Convert n into num array in reverse order
- Find the leftmost position that is inverted and if the inverted character repeats itself, find the leftmost repeated digit.
- Fill the digits after inversion as 9
- Reduce the digit that caused the inversion by -1
- 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]的更多相关文章
- [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 ...
- 【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 m ...
- 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 ...
- [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 ...
随机推荐
- iOS7下Status Bar字体颜色修改
原文来自这里:iOS7下Status Bar字体颜色修改. 旧项目在iOS7上遇到status bar字体颜色需要修改的问题,症状如下:导航栏设置为黑色后,iphone上status bar的字体颜色 ...
- 『ACM C++』 Codeforces | 1003C - Intense Heat
今日兴趣新闻: NASA 研制最强推进器,加速度可达每秒 40 公里,飞火星全靠它 链接:https://mbd.baidu.com/newspage/data/landingsuper?contex ...
- python 创建虚拟环境
创建一个文件夹:mkdir tf_env 进入到文件夹内:cd tf_env 创建虚拟环境:python3 -m venv tensorflow-dev 激活虚拟环境:source tensorflo ...
- Python模块、包、异常、文件(案例)
Python模块.包.异常.文件(案例) python.py #模块 # Python中的模块(Module),是一个Python文件,以.py文件结尾,包含了Python对象定义和Python语句, ...
- webuploader的一个页面多个上传按钮实例
借鉴一位大佬的demo 附上他的github地址https://github.com/lishuqi 我把他的cxuploader.js改了不需要预览 直接上传图片后拿到回传地址给img标签显示图 ...
- player视频.js
var playStatus = 'pending'; var html_a = '<div class="weui-dialog__bd" id="lly_dia ...
- Centos配置网卡子接口
1.检查OS是否加载802.1q模块: 方法一: [root@rs2 ~]# modinfo 8021q 方法二: [root@rs2 ~]# modinfo -F filename 8021q 方法 ...
- VMware中Linux的NAT相关配置
我这边使用的是CentOS7,因为搭建环境主要是为了后期做集群,所以会搭建三台机器master,slave1,slave2. 一.设置固定IP 1. 进入到/etc/sysconfig/network ...
- Java基础—IO小结(二)缓冲流与其它流的使用
一.缓冲流的使用 每个字节流都有对应的缓冲流: BufferedInputStream / BufferedOutputStream 构造器: 方法摘要与对应节点流类似 使用缓冲流实现文件复制:实际中 ...
- 北京Uber优步司机奖励政策(12月28日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...