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

标签(空格分隔): LeetCode

作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/monotone-increasing-digits/description/

题目描述:

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

题目大意

找出一个比这个数字小的,每位数字都是递增的最大数字。

解题方法

把题目给出的数字拆分成数组进行理解。如果这么理解之后,那我们就是要找到一个最大的递增数组。

做的方法是找出第一个降序的位置,有两种情况:

case 1:
对于 14267 , 第一个出现下降的位置是4,所以把4变成3,把4后面的数字全部改成9.得到13999;

case 2:
对于1444267, 第一个降序的位置是最后一个4,如果只把最后一个4按照case1处理,那么得到的是1443999,仍然不满足题意。所以需要找到第一个位置的4,然后做case1操作,这样得到的是1399999。

写代码的时候我是逆序过来做的,然后我犯了一个错误,写成了num[i] > num[i - 1] or (ind != -1 and num[i] == num[i - 1])。为什么不能是num[i] == num[i - 1]呢?因为这样会找到最后的一个相等的位置,而我们只需要找到最先出现相等的位置即可。

时间复杂度是O(n),空间复杂度是O(n).

代码如下:

class Solution:
def monotoneIncreasingDigits(self, N):
"""
:type N: int
:rtype: int
"""
if N < 10: return N
num = [int(n) for n in str(N)[::-1]]
n = len(num)
ind = -1
for i in range(1, n):
if num[i] > num[i - 1] or (ind != -1 and num[i] == num[ind]):
ind = i
if ind == -1:
return N
res = '9' * ind + str(num[ind] - 1) + "".join(map(str, num[ind + 1:]))
return int(res[::-1])

如果不使用逆序,也可以做:

class Solution:
def monotoneIncreasingDigits(self, N):
"""
:type N: int
:rtype: int
"""
if N < 10: return N
num = [int(n) for n in str(N)]
n = len(num)
ind = n - 1
for i in range(n - 2, -1, -1):
if num[i] > num[i + 1] or (ind != n - 1 and num[i] == num[ind]):
ind = i
if ind == n - 1:
return N
num[ind] -= 1
for i in range(ind + 1, n):
num[i] = 9
return int("".join(map(str, num)))

参考资料:

https://leetcode.com/problems/monotone-increasing-digits/discuss/109794/Simple-Python-solution-w-Explanation

日期

2018 年 9 月 16 日 ———— 天朗气清,惠风和畅

【LeetCode】738. Monotone Increasing Digits 解题报告(Python)的更多相关文章

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

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

  7. 【LeetCode】 258. Add Digits 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:减1模9 方法三:直接模9 日 ...

  8. 【LeetCode】491. Increasing Subsequences 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. 【LeetCode】788. Rotated Digits 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. GlimmerHMM指南

    GlimmerHMM指南 官方用户手册 GlimmerHMM是一种De novo的新基因预测软件. 新基因发现基于Generalized Hidden Markov Model (GHMM). Gli ...

  2. pyquery解析库的介绍和使用

    ### pyquery的介绍和使用 ## 测试文本 text = ''' <html><head><title>there is money</title&g ...

  3. 深入浅出KMP

    前言:曾经有次在阿里的面试中遇到这个基础的问题,当时知道有这么回事,可是时间久了便 想不起来,可能是不怎么用到,基本调用库什么的,还有个是理解不深刻,不能得到show me the code 的程度, ...

  4. vim文本编辑器的基本使用

    vim文本编辑器的基本使用 1. vi和vim的区别和联系 可以说vim是vi的增强版,在使用vim编辑文本时,可以根据字体颜色来判断编写程序的正确性. 2. vim文本编辑器的常用命令 1. 编辑指 ...

  5. 【XSS】再谈CSP内容安全策略

    再谈CSP内容安全策略 之前每次都是想的很浅,或者只是个理论派,事实证明就是得动手实践 参考 CSP的用法 官方文档 通过设置属性来告诉浏览器允许加载的资源数据来源.可通过Response响应头来设置 ...

  6. Ganglia 简单介绍与安装

    文章来至于   http://sachinsharm.wordpress.com/2013/08/17/setup-and-configure-ganglia-3-6-on-centosrhel-6- ...

  7. 【J-Link】J-Link不支持(版本太低)

    事情起因,我原本可以烧录和仿真的(版本6.3.4),但是后来安装另一个东西,这个东西里面包含旧的J-Link驱动(版本5.1.2) 它把Keil文件夹下的JLinkARM.dll覆盖了,导致出现下面的 ...

  8. Dubbo多版本控制

    当系统进行升级时,一般都是采用"灰度发布(又称为金丝雀发布)"过程.即在低压力时段,让部分消费者先调用新的提供者实现类,其余的仍然调用老的实现类,在新的实现类运行没有问题的情况下, ...

  9. 实时数据同步inotify+rsync

    inotify inotify是一个实时监控服务,他能实时监控服务器中的目录的变化,发现目录中变化后,在配合rsync服务推送到备份服务器上 inotify要求内核要在2.6.13或之上,通过noti ...

  10. Windows下80端口被占用的解决方法(SQL Server)

    查找80端口被谁占用的方法 进入命令提示行(WIN+R 输入 CMD),输入命令 netstat -ano|findstr 80 (显示包含:80的网络连接) ,就可以看到本机所有端口的使用情况,一般 ...