[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 ...
随机推荐
- SQLMAP使用详解
使用示例 python sqlmap.py -u "http://xx.com/member.php?id=XX" -p id --dbms "Mysql" ...
- React通过dva-model-extend实现 dva 动态生成 model
前言 实现通过单个component 单个router通过相应的标识对应产生不同model实现数据包分离,model namespce将会覆盖基础的Model,其中的model[state|subsc ...
- 在vue-cli + webpack 项目中使用sass
1.准备工作: 由于npm的服务器在国外,网速慢而且安装容易失败,建议在安装之前,先安装国内的镜像,比如淘宝镜像 npm install -g cnpm --registry=https://regi ...
- Canvas状态的保存与恢复
Canvas的API提供了save()和restore()的方法,用于保存及恢复当前canvas绘图环境的所有属性. save()与restore()方法可以嵌套调用 save()方法将当前绘图环境压 ...
- Spring异常重试框架Spring Retry
Spring Retry支持集成到Spring或者Spring Boot项目中,而它支持AOP的切面注入写法,所以在引入时必须引入aspectjweaver.jar包. 快速集成的代码样例: @Con ...
- 7、Linux应用程序地址布局
程序构成 在学习Linux应用程序开发时,经常会遇到如下概念: 代码段.数据段.BSS段(Block Started by Symbol,又名:未始化数据段) .堆(heap)和栈(stack).始化 ...
- Python学习:7.文件操作
文件操作 我们曾将听过一个问题,将大象放入冰箱分为三步:1.打开冰箱门,2.将大象放进去,3.关上冰箱门.今天我们要讲的Python文件操作的步骤就像将大象放入冰箱的步骤一样. 使用Python操作文 ...
- C#、C++、Java、Python 选择哪个好?
C#.C++.Java.Python 选择哪个好? 2019年03月06日 16:54:34 编程小火车 阅读数:214 首先排除Python,光动态语言一个理由,就已经万劫不复了.无论有多少所谓 ...
- 利用cross-entropy cost代替quadratic cost来获得更好的收敛
1.从方差代价函数说起(Quadratic cost) 代价函数经常用方差代价函数(即采用均方误差MSE),比如对于一个神经元(单输入单输出,sigmoid函数),定义其代价函数为: 其中y是我们期望 ...
- print&input--Python
1.print --> 打印到屏幕输出字符串 print("this is a dog!") -->代码 D:\Python\venv\Scripts\python.e ...