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. python学习(三) 使用字符串

                                                                                               第三章 使用字符串 ...

  2. Sql Server优化之路

    本文只限coder级别层次上对Sql Server的优化处理简结,为防止专业DB人士有恶心.反胃等现象,请提前关闭此页面. 首先得有一个测试库,使用数据生成计划生成测试数据库(参考:http://de ...

  3. java成神之——集合框架之ArrayList,Lists,Sets

    集合 集合种类 ArrayList 声明 增删改查元素 遍历几种方式 空集合 子集合 不可变集合 LinkedList Lists 排序 类型转换 取交集 移动元素 删除交集元素 Sets 集合特点 ...

  4. NetBeans配置Xdebug 远程调试PHP

    1.配置PHP 说明:xdebug.trace_output_dir和xdebug.profiler_output_dir需要增加权限 #chmod 755 /usr/xdebug-tmp xdebu ...

  5. SSD知识

    不管什么接口的SSD,一般都由以下部分组成:主控,Flash,板,壳,品牌.下面本佬就这些部分一一发帖,仅供娱乐参考,不作任何推荐和偏向,有不同见解请直接发表,有任何错误,请直接指正,不为吵架,只为娱 ...

  6. JS四级复选框选中层次关系

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. c# 类型化变量的var,和javascript一样的使用

    .net framework推出var关键字,注意是关键字,不是什么新类型,只是编译器抛给我们的“语法糖” 1.object是所有类型的父类型,object a=1;和var a=1:的区别在于前一个 ...

  8. mahout in Action2.2-给用户推荐图书(2)-分析对用户推荐书目的结果

    2.2.3 Analyzing the output 在之前的程序运行结果中我们得到的结果输出是: RecommendedItem [item:104, value:4.257081] 程序要求选择一 ...

  9. RedHat&nbsp;Enterprise&nbsp;Linu…

    Abstract 在嵌入式开发中有宿主机和目标机之分:宿主机是执行编译.链接嵌入式软件的计算机:目标机是运行嵌入式软件的硬件平台. TFTP服务器作为工作于宿主机的软件,主要提供对目标机的主要映像文件 ...

  10. Windows下Git中正确显示中文的设置方法

    Windows下Git中正确显示中文的设置方法 具体设置方法如下: 进入目录etc:$ cd /etc 1. 编辑 gitconfig 文件:$ vi gitconfig.在其中增加如下内容: [gu ...