LeetCode 788 Rotated Digits 解题报告
题目要求
X is a good number if after rotating each digit individually by 180 degrees, we get a valid number that is different from X. Each digit must be rotated - we cannot choose to leave it alone.
A number is valid if each digit remains a digit after rotation. 0, 1, and 8 rotate to themselves; 2 and 5 rotate to each other; 6 and 9 rotate to each other, and the rest of the numbers do not rotate to any other number and become invalid.
Now given a positive number N
, how many numbers X from 1
to N
are good?
题目分析及思路
给定一个正整数范围,要求得到good number的个数。good number的定义是:对该数的各位数进行180度翻转,最后得到的数字与原数不同。这里,0,1,8翻转后还是本身,2,5,6,9翻转后可得不同的有效数字,剩余数字翻转则无效。可以先遍历这个范围的每一个数,并获得该数的各位数,最后只要确定各位数中没有3,4,7且有2,5,6,9即可确定该数为good number。
python代码
class Solution:
def rotatedDigits(self, N: int) -> int:
count = 0
for n in range(2,N+1):
digits = []
while n:
digit = n % 10
digits.append(digit)
n = n // 10
if not (set(digits) & set([3,4,7])) and (set(digits) & set([2,5,6,9])):
count += 1
return count
最新文章
- UEFI引导在GPT分区下安装win2008——抓住那只傲娇的win2008
- object-c NSString 转成特定编码格式如utf8、gbk等
- Qt xcode wrapper Qios OpenFly
- 怎么创建MongoDB数据库
- jquery delegate
- 20151212jquery学习笔记--工具函数
- ASP.NET MVC5总结(二)@HTML扩展
- js/jquery/插件表单验证
- Generator &; Co
- 使用传入的总记录数实现一条sql语句完成分页查询
- iOS 之 设置横竖屏及ViewWillAppear及视图与导航控制器嵌套时的不同反应
- Python 练习册,每天一个小程序----第0000题
- Netty学习路线
- Eclipse 添加 lib (导入 .jar 包)
- req和resp常用的方法
- 【Windows 7】发现一个奇怪的现象
- bootstrap-treeview 如何实现全选父节点下所有子节点及反选
- 关于Jedis是否线程安全的测试
- Pycharm学习python路
- DateGridView标题列头添加复选框
热门文章
- make[1]: *** [storage/perfschema/unittest/CMakeFiles/pfs_connect_attr-t.dir/all] 错误 2 解决方法
- 天兔(Lepus)数据库监控系统快速安装部署
- 如何保证修改resolv.conf后重启不恢复?
- XML格式化工具
- Jupyter/JupyterLab安装使用
- [Java并发编程(一)] 线程池 FixedThreadPool vs CachedThreadPool ...
- 大批量delete 优化方案
- C# SMTP 邮件发送之QQ邮箱篇
- acl &;&; prefix list
- minicom for Mac 配置