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 ...
随机推荐
- Android 4 学习(10):Adapters简介
参考<Professional Android 4 Development> Adapters简介 Adapter用于将数据和实现AdapterView接口的ViewGroup绑定在一起. ...
- Asp.Net framework 类库 自带的缓存 HttpRuntime.Cache HttpContext.Cache
两个Cache 在.NET运用中经常用到缓存(Cache)对象.有HttpContext.Current.Cache以及HttpRuntime.Cache,HttpRuntime.Cache是应用程序 ...
- MySql 里的IFNULL、NULLIF、ISNULL和IF用法
isnull(expr) 的用法: 如expr 为null,那么isnull() 的返回值为 1,否则返回值为 0. 实例: select ISNULL(NULL) 输出结果: ) 输出结果: IFN ...
- Uboot详细解析1
uboot 详细注释讲解 声明:该贴是通过参考其他人的帖子整理出来,从中我加深了对uboot的理解,我知道对其他人一定也是有很大的帮助,不敢私藏,如果里面的注释有什么错误请给我回复,我再加以修改.有些 ...
- C++中不可重载5个运算符
C++中不可重载的5个运算符 C++中的大部分运算符都是可以重载的,只有以下5个运算符不可以重载,他们是: 1 .(点运算符)通常用于去对象的成员,但是->(箭头运算符),是可以重载的 2 ...
- 在java中RandomAccessFile类的作用:对指定文件可以进行读写的操作
- 读书笔记 Week5 2018-4-5
再结束了第一个个人任务以后,我也算有点时间翻开一本大部头来通读一下.在看了一些相关的评论说:“该书可以从任意章节读起”后,刚刚在180M测试文件的个人任务中吃了亏的我,决定从他的第5部分,代码改善看起 ...
- UVA-11280 Flying to Fredericton
题意 给定一些国家,和两个国家间的花费,现在有一些询问,询问每次最多转k次飞机,最小花费 分析 最短路的裸题,跑spfa或者dijsktra什么的都行 多开一维来记录转k次飞机时的最短路是什么(拆点? ...
- 优化tomcat配置(从内存、并发、缓存3个方面)优化
Tomcat有很多方面,我从内存.并发.缓存三个方面介绍优化方法. 一.Tomcat内存优化 Tomcat内存优化主要是对 tomcat 启动参数优化,我们可以在 tomcat 的启动脚本 catal ...
- 获取文件的后缀名。phpinfo
1: function get_extension($file){ //strrchr 返回 .jpg substr :1 是从1开始. substr(strrchr($file,'.'),1) } ...