- 921. Minimum Add to Make Parentheses Valid 使括号有效的最少添加
- 915. Partition Array into Disjoint Intervals 分割数组
- 932. Beautiful Array 漂亮数组
- 940. Distinct Subsequences II 不同的子序列 II
题目地址: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])
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
如果不使用逆序,也可以做:
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)))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
参考资料:
https://leetcode.com/problems/monotone-increasing-digits/discuss/109794/Simple-Python-solution-w-Explanation
DDKK.COM 弟弟快看-教程,程序员编程资料站,版权归原作者所有
本文经作者:负雪明烛 授权发布,任何组织或个人未经作者授权不得转发
我正在实现一个算法,当用户输入字符串时,字符串中的每个字符(如果是字母表)都应该增加给定的值(这里是旋转器)。我正在玩这个代码 2 小时,但无法弄清楚为什么当我按值旋转器递增时,它会按 rotator
我有 1.0.5。我怎样才能增加到 1.0.6? 试过了,但是不行。 echo 1.0.5 0.0.1 | awk '{sum=$1+$2; printf"%0.2f\n", sum }' 最佳答案
这个问题在这里已经有了答案: Behaviour of increment and decrement operators in Python (11 个回答) Why are there no ++
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎偏离主题,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或 include a mini
我正在尝试温习我的 C,我有以下代码,当我使用 i 偏移量但不使用 Hold++ 时,它可以工作,我不明白为什么我认为他们做了同样的事情?这是我无法开始工作的版本: char* reversestri
我需要增加/减少 PostgreSQL 数据库中的计时。 下面是包含列类型为"timestamp without time zone" 的表的输出 如果时间超过 24 小时,我也需要更改日期。请协助
我有一个名为 temp_rfm 的表,其中 col1 实际上是客户 ID(我有一个非法的联盟混合问题)和 calc_date 是增加月份的开始。 SELECT * FROM temp_rfm ; co
我目前正在处理我的应用程序的首选项,我必须设置一个角度。默认值约为 30°,用户应该能够调整此角度以使其最适合。 我不只是制作一个普通的 EditTextPreference,而是希望它可以像在其他应
这个问题已经有答案了: Increment a number by prefix and postfix operator (1 个回答) 已关闭去年。 我正在努力理解 Javascript 增量运算
我使用下面的 javascript 递归地重新加载一个目标 DIV,其 id="outPut",将参数传递给 getData.php 时执行数据查询的结果>。问题是 fadeTo 会淡化每次迭代调用返
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Jquery Draggable + Bring to Front 我有一个网站,用户可以在其中打开多个聊天
我必须定义一个函数,其中: Starting with a positive integer original, keep multiplying original by n and calculat
我正在我的应用程序中记录一些统计数据。其中一项统计数据是 BigDataStructure 的大小。我有两个选择: 创建一个计数器并递增/每次递减计数器有一个添加/删除大数据结构。 每次添加/删除从
在下面的 Java 示例程序中,我得到了无限循环,我不明白为什么: public class Time { public static int next(int v) { re
我从 C#/WPF 添加了一个意外的行为 private void ButtonUp_Click(object sender, RoutedEventArgs e) {
我想在 Python 2.7 中增加用户提供的字符串的最后一位数字。 我可以这样替换第一个数字: def increment_hostname(name): try: numb
我正在用蛮力搜索具有某些属性的 float (sin(a)^2+cos(a)^2-1 的小舍入误差)。因此,我想通过递增尾数来遍历给定 float 的邻域。 在 C 中是否有一种简单的方法可以做到这一
C 标准将 _Bool 定义为包含 0 或 1 的无符号类型。如果 _Bool 类型的值 1 递增,据我所知,有两个选项: 该值在 1 到 0 之间环绕 该值增加到 2,它是非零值,因此在转换回 _B
我有一个 INI 文件,其中存储了一些用于设置的整数。部分名称存储如下: [ColorScheme_2] name=Dark Purple Gradient BackgroundColor=224 B
我的应用程序中有这个方法: - (void)initializeTimer{ self.myTimer = [NSTimer scheduledTimerWithTimeInterval:th
我是一名优秀的程序员,十分优秀!