【leetcode】Monotone Increasing Digits
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].
解题思路:本题要求的是找出一个前一位不大于后一位的整数,并且这个整数不能大于输入的数。最直接的方法,是从输入数开始判断,依次减一,直到找到符合条件的数为止。更为简便的方法可以这样,从输入数的最低位开始依次和前一位比较,如果前一位大于后一位,那么前一位减1,后面所有位的值都置为9,直到遍历到最高位为止。例如输入数为762543。从最低位3开始找,次低位的是4小于5,把4和3都置为9,5减1变成4。第一次变换后的值为762499,继续找到6小于2,所以变换成759999,最后5小于7,变成699999。
代码如下:
class Solution(object):
def monotoneIncreasingDigits(self, N):
"""
:type N: int
:rtype: int
"""
sn = str(N)
sn = sn[::-1]
l = []
for i in sn:
l.append(int(i))
for i in range(len(l)-1):
if l[i] >= l[i+1]:
continue
else:
#l[i] = 9
for j in range(i+1):
l[j] = 9
l[i + 1] -= 1
res = 0
count = 0
for i in l:
res += i*pow(10,count)
count += 1 return res
【leetcode】Monotone Increasing Digits的更多相关文章
- 【LeetCode】334. Increasing Triplet Subsequence 解题报告(Python)
		
[LeetCode]334. Increasing Triplet Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
 - [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 ...
 - 【leetcode】1291. Sequential Digits
		
题目如下: An integer has sequential digits if and only if each digit in the number is one more than the ...
 - 【LeetCode】 258. Add Digits 解题报告(Java & Python)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:减1模9 方法三:直接模9 日 ...
 - 【LeetCode】491. Increasing Subsequences 解题报告(Python)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
 - 【LeetCode】258. Add Digits (2 solutions)
		
Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...
 - 【LeetCode】258. Add Digits
		
题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...
 - 【LeetCode】788. Rotated Digits 解题报告(Python)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
 - 【LeetCode】897. Increasing Order Search Tree 解题报告(Python)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 重建二叉树 数组保存节点 中序遍历时修改指针 参考资 ...
 
随机推荐
- elasticsearch索引大小写的问题
			
今天用elasticsearch创建索引时碰到一个问题,用大写创建的索引,ES前端插件可以看到用大写创建索引存入的数据 程序启动,搜索的时候,却不显示数据,索引变成了小写,导致数据无法封装传入. 在h ...
 - 【HANA系列】SAP HANA Studio代码提示设置方法
			
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA Studio代 ...
 - Linux字符界面字符颜色显示
			
一.字符颜色 #!/bin/bash #字符颜色显示 #-e:允许echo使用转义 #\033[:开始位 #\033[0m:结束位 #\033等同于\e echo -e "\033[30m黑 ...
 - PTA(Basic Level)1031.查验身份证
			
一个合法的身份证号码由17位地区.日期编号和顺序编号加1位校验码组成.校验码的计算规则如下: 首先对前17位数字加权求和,权重分配为:{7,9,10,5,8,4,2,1,6,3,7,9,10,5,8, ...
 - PHP生成excle文件
			
excle_word.php /*** * 生成excle文件 ***/ $time = time(); $filename = date("Y年m月d日h点m分s秒", $tim ...
 - WebDriverWait类以及类常用的方法
			
WebDriverWait类提供了显式等待和隐式等待,显式等待的等待时间是固定的,固定了10s就必须等待10s,隐式等待的等待时间是个范围,例如最大10s,那么如果在3s的时候程序达到预期的结果,那么 ...
 - Win10下编译OpenJDK8
			
导航目录 Win10下编译OpenJDK8 相关参考文章 编译环境 编译前准备 1.安装 Visual Studio 2010 Professional 2. 准备OpenJDK8 3. 编译JDK环 ...
 - NSIS打包软件使用
			
NSIS打包软件使用 最近做的一个调用远程桌面的小应用需要打个安装包,并且在安装的时候需要添加注册表,我之前使用的都是"Advanced Installer"来打包应用程序的,这个 ...
 - Linux 查看系统相关信息(系统发型版本及内核版本等)
			
以下为 Linux 以下信息查看命令,记录以备用 # uname -a # 查看内核/操作系统/CPU信息 # /etc/issue # 查看操作系统版本 # cat /proc/cpuinfo # ...
 - BZOJ 4141 [Thu Summer Camp 2013]魔塔
			
权限题qwq 以下设值域大小为\(m\) 先考虑枚举攻击力,因为首先攻击力决定每个怪物的攻击次数,然后对于每个怪物,攻击次数为\(\lceil\frac{hp_i}{ATK-def_i}\rceil\ ...