【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. 单片机ISP、IAP和ICP几种烧录方式的区别

    单片机ISP.IAP和ICP几种烧录方式的区别 玩单片机的都应该听说过这几个词.一直搞不太清楚他们之间的区别.今天查了资料后总结整理如下. ISP:In System Programing,在系统编程 ...

  2. CPU如何同时运行多个进程?

    1 # -*- coding: utf-8 -*- 2 import re 3 mem = [x for x in re.split('[\r|\n]', ''' 4 store a 1 5 add ...

  3. Flink(九)【Flink的重启策略】

    目录 1.Flink的重启策略 2.重启策略 2.1未开启checkpoint 2.2开启checkpoint 1)不设置重启策略 2)不重启 3)固定延迟重启(默认) 4)失败率重启 3.重启效果演 ...

  4. Sharding-JDBC 实现水平分表

    1.搭建环 (1) 技术: SpringBoot2.2.1+ MyBatisPlus + Sharding-JDBC + Druid 连接池(2)创建 SpringBoot 工程

  5. Kafka 架构深入

    Kafka 工作流程及文件存储机制

  6. iOS 的文件操作

    直接上操作 效果:将一张图片写入文件 (图片本身已经在Assets.xcassets里面了) 1.获取当前app的沙盒路径 NSString *documentPath = NSSearchPathF ...

  7. spring cloud 通过 ribbon 实现客户端请求的负载均衡(入门级)

    项目结构 环境: idea:2020.1 版 jdk:8 maven:3.6.2 1. 搭建项目 ( 1 )父工程:spring_cloud_demo_parent pom 文件 <?xml v ...

  8. 6.Vue.js-条件与循环

    条件判断 v-if 条件判断使用 v-if 指令: <div id="app"> <p v-if="seen">现在你看到我了</ ...

  9. 修复Apache Log4j任意代码执行漏洞安全风险通告

    2021年12月10日 0x01漏洞背景 Apache Log4j 是 Apache 的一个开源项目,Apache Log4j2是一个基于Java的日志记录工具.该工具重写了Log4j框架,并且引入了 ...

  10. Excel字符串函数公式大全

    一.Excel字符串的操作  1.1.Excel根据字节截取对应字符串(注:一个中文汉字对应两个字节) =LEFTB(A3,7) 从左边开始截取7个字节 =RIGHTB(A10,10) 从右边开始截取 ...