【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:

  1. Input: N = 10
  2. Output: 9

Example 2:

  1. Input: N = 1234
  2. Output: 1234

Example 3:

  1. Input: N = 332
  2. 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).

代码如下:

  1. class Solution:
  2. def monotoneIncreasingDigits(self, N):
  3. """
  4. :type N: int
  5. :rtype: int
  6. """
  7. if N < 10: return N
  8. num = [int(n) for n in str(N)[::-1]]
  9. n = len(num)
  10. ind = -1
  11. for i in range(1, n):
  12. if num[i] > num[i - 1] or (ind != -1 and num[i] == num[ind]):
  13. ind = i
  14. if ind == -1:
  15. return N
  16. res = '9' * ind + str(num[ind] - 1) + "".join(map(str, num[ind + 1:]))
  17. return int(res[::-1])

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

  1. class Solution:
  2. def monotoneIncreasingDigits(self, N):
  3. """
  4. :type N: int
  5. :rtype: int
  6. """
  7. if N < 10: return N
  8. num = [int(n) for n in str(N)]
  9. n = len(num)
  10. ind = n - 1
  11. for i in range(n - 2, -1, -1):
  12. if num[i] > num[i + 1] or (ind != n - 1 and num[i] == num[ind]):
  13. ind = i
  14. if ind == n - 1:
  15. return N
  16. num[ind] -= 1
  17. for i in range(ind + 1, n):
  18. num[i] = 9
  19. 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. mysql-日期时间函数大全

    DAYOFWEEK(date)  返回日期date是星期几(1=星期天,2=星期一,--7=星期六,ODBC标准)mysql> select DAYOFWEEK('1998-02-03');  ...

  2. nordic 51822 sdk. timer 的使用

    它的源代码和头文件分别为app_timer.c/app_timer.h.这是Nordic为我们提供的虚拟定时器,这个定时器不同于硬件上的TIMER,而是基于RTC1实现的一种虚拟定时器,其将定时功能作 ...

  3. Go语言核心36讲(Go语言实战与应用二十二)--学习笔记

    44 | 使用os包中的API (上) 我们今天要讲的是os代码包中的 API.这个代码包可以让我们拥有操控计算机操作系统的能力. 前导内容:os 包中的 API 这个代码包提供的都是平台不相关的 A ...

  4. 多线程高级篇1 — JUC — 只弄到处理高并发集合问题

    1.线程池 1.1).什么是线程池? 池( pool ),就是一个容器,所以线程池就是把多个线程对象放到一个容器中 1.2).如何创建线程池? 先来了解几个常识 Executor -- 这是一个接口( ...

  5. GO并发相关

    锁的使用 注意要成对,重点是代码中有分支或者异常返回的情况,这种情况要在异常返回前先释放锁 mysqlInstanceLock.Lock() slaveHostSql := "show sl ...

  6. oc中调用c函数 实现将字符串转换成unsigned char

    帮助码友解决问题,从而复习了一下oc中调用c函数的方式 1,新建c 头文件  test.h 定义 c 函数 #ifndef test_h #define test_h void verificatio ...

  7. 编译安装haproxy2.0

    先解决lua环境,(因为centos自带的版本不符合haproxy要求的最低版本(5.3)先安装Lua依赖的包 [root@slave-master lua-5.3.5]# yum install  ...

  8. maven根据profile,resources,filters来区分部署环境

    项目过程中,在不同的阶段,分别需要部署开发环境,测试环境,线上环境.如果都用一套配置文件,很容易弄乱,所以维持多套配置文件很有必要. maven提供了一组属性以供开发人员灵活搭配,可以根据环境来打包, ...

  9. js 长按鼠标左键实现溢出内容左右滚动滚动

    var nextPress, prevPress; // 鼠标按下执行定时器,每0.1秒向左移一个li内容的宽度 function nextDown() { nextPress = setInterv ...

  10. 使用Spring Data ElasticSearch框架来处理索引

    /**步骤:创建工程,导入相应的包--->配置文件---->创建实体类对象------>创建接口---->测试增删改查的方法 **/ //步骤:创建工程,导入相应的包 < ...