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].
Approach #1: C++. [recursive]
class Solution {
public:
int monotoneIncreasingDigits(int N) {
stack<int> temp;
while (N) {
int lastNum = N % 10;
N /= 10;
temp.push(lastNum);
}
int curNum = temp.top();
temp.pop();
int ans = 0;
while (!temp.empty()) {
if (curNum <= temp.top()) {
ans = ans * 10 + curNum;
curNum = temp.top();
temp.pop();
} else {
ans = ans * 10 + curNum - 1;
for (int i = 0; i < temp.size(); ++i)
ans = ans * 10 + 9;
if (judge(ans))
return ans;
else return monotoneIncreasingDigits(ans);
}
}
ans = ans * 10 + curNum;
return ans;
}
bool judge(int N) {
int lastNum = N % 10;
N /= 10;
while (N) {
int curNum = N % 10;
N /= 10;
if (curNum > lastNum) return false;
lastNum = curNum;
}
return true;
}
};
Approach #2: Java. [Truncate After Cliff]
class Solution {
public int monotoneIncreasingDigits(int N) {
char[] S = String.valueOf(N).toCharArray();
int i = 1;
while (i < S.length && S[i-1] <= S[i]) i++;
while (0 < i && i < S.length && S[i-1] > S[i]) S[--i]--;
for (int j = i+1; j < S.length; ++j) S[j] = '9';
return Integer.parseInt(String.valueOf(S));
}
}
738. Monotone Increasing Digits的更多相关文章
- 【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 ...
- [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 ...
- 738. Monotone Increasing Digits 单调递增的最接近数字
[抄题]: Given a non-negative integer N, find the largest number that is less than or equal to N with m ...
- 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】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]
Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...
随机推荐
- FusionCharts simple demo for (html+js、APS.NET Webform、MVC)
做GIS或其他内部数据统计项目的应该对FusionCharts也不会太陌生,简单易用已无需多说什么了,只是有时候框架不同,实现起来也稍有差异 引用dll调用FusionCharts类的静态方法Rend ...
- HTTP协议头域详解
HTTP协议头域详解 Requests部分 Accept 指定客户端能够接收的内容类型 Accept: text/plain, text/html Accept-Charset 浏览器可以接受的字符编 ...
- Android 4 学习(12):Linkify & Broadcast event
参考<Professional Android 4 Development> Linkify Linkfy类可以在Text View中创建超链接.匹配LInkify中正则表达式的文本将被L ...
- jquery-uploadify传参方法
jquery-uploadify传参方法$(document).ready(function () { $("#uploadify").uploadify({ 'uploader' ...
- 创建maven工程时报错,解决方案
解决思路:在本地库中装载maven-archetype-quickstart 1.从 http://mirrors.ibiblio.org/pub/mirrors/maven2/org/apache/ ...
- LINQ GroupBy 查询数据赋给select
roles.GroupBy(a => new { a.SubjectID,a.SubjectName}).Select(p => new SelectListItem() { Value ...
- DLL卸载
[DLL卸载] 1.扫描Module.通过CreateToohelp32Snapshot.Module32First.Module32Next来完成. 2.通过FreeLibrary来卸载.通过在ke ...
- Gym101350 FMonkeying Around
题意 有n只猴子排成一排,一共有m个笑话.开始时,这些猴子都坐在椅子上.下面m行给出的每个笑话包含三个整数x,l,k.代表猴子x讲了笑话l,所以距离x小于等于k的猴子如果他们从没听过这个笑话,他们会掉 ...
- 7. Reverse Integer 反转整数
[抄题]: 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数). 样例 给定 x = 123,返回 321 给定 x = -123,返回 -321 [暴力解法]: ...
- 在Python中操作谷歌浏览器
在Python中使用谷歌浏览器,注意以下几点: 1.下载安装的谷歌浏览器Chrome和驱动chromedriver.exe要版本一致. 2.驱动chromedriver.exe要放在Chrome浏览器 ...