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的更多相关文章

  1. 【LeetCode】738. Monotone Increasing Digits 解题报告(Python)

    [LeetCode]738. Monotone Increasing Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...

  2. 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 ...

  3. [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 ...

  4. 738. Monotone Increasing Digits 单调递增的最接近数字

    [抄题]: Given a non-negative integer N, find the largest number that is less than or equal to N with m ...

  5. 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 ...

  6. [LeetCode] Monotone Increasing Digits 单调递增数字

    Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...

  7. [Swift]LeetCode738. 单调递增的数字 | Monotone Increasing Digits

    Given a non-negative integer N, find the largest number that is less than or equal to Nwith monotone ...

  8. 【leetcode】Monotone Increasing Digits

    Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...

  9. [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 ...

随机推荐

  1. Linux quota命令参数及用法详解---Linux磁盘配额限制设置和查看命令

    功能说明:显示磁盘已使用的空间与限制. 语 法:quota [-quvV][用户名称...] 或 quota [-gqvV][群组名称...] 补充说明:执行quota指令,可查询磁盘空间的限制,并得 ...

  2. ndnarry矩阵处理

    ndarray的矩阵运算 数组是编程中的概念,矩阵.矢量是数学概念. 在计算机编程中,矩阵可以用数组形式定义,矢量可以用结构定义! 1. 矢量运算:相同大小的数组间运算应用在元素上 示例代码(1): ...

  3. tomcat注册成windows系统服务

    一.下载Tomcat Tomcat可以从http://tomcat.apache.org/网站下载,选择任意版本,在 Binary Distributions 下的zip包既是. 二.配置Tomcat ...

  4. 分布式爬虫搭建系列 之三---scrapy框架初用

    第一,scrapy框架的安装 通过命令提示符进行安装(如果没有安装的话) pip install Scrapy 如果需要卸载的话使用命令为: pip uninstall Scrapy 第二,scrap ...

  5. Protege4.3使用入门(二)

    1.支持OWLViz 利用OWLViz查看我们构建Class的结构图.如果尚未安装,请到http://www.graphviz.org/pub/graphviz/stable/windows/grap ...

  6. 使用K2时提示未能加载文件或程序集Microsoft.IdentityModel等

    转:http://www.cnblogs.com/dannyli/archive/2012/10/15/2724931.html K2安装成功后,打开workspace管理流程时报错如下图: 未能加载 ...

  7. 「小程序JAVA实战」 小程序抽离公用方法进行模块化(12)

    转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-12/ 小程序的模块化,把砖磊成一个墩子,用的时候把整个墩子移走.js更好的调用,应用更加公用化.源 ...

  8. quartz在web.xml的配置

    第一步:下载所需的Jar包 commons-beanutils.ja.commons-collections.jar.commons-logging.jar.commons-digester.jar. ...

  9. 专利系统数据库连接出现 base-64字符串中的无效字符 错误

    错误提示如图: 解决方法: 1.进注册表修改如下 2.进入系统配置页面http://10.10.0.70/eaf/init 对数据库进行重新配置 3.若不行再将如下密码修改一下 重启IIS生效

  10. java基础篇之HashMap

    HashMap和C#中的Dictionary基本一样 键是唯一值 值可以是对象 循环HashMap的方式:一: 1,通过Set<T> st = hs.keySet()找到所有的key值集合 ...