题目要求

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

最新文章

  1. UEFI引导在GPT分区下安装win2008——抓住那只傲娇的win2008
  2. object-c NSString 转成特定编码格式如utf8、gbk等
  3. Qt xcode wrapper Qios OpenFly
  4. 怎么创建MongoDB数据库
  5. jquery delegate
  6. 20151212jquery学习笔记--工具函数
  7. ASP.NET MVC5总结(二)@HTML扩展
  8. js/jquery/插件表单验证
  9. Generator & Co
  10. 使用传入的总记录数实现一条sql语句完成分页查询
  11. iOS 之 设置横竖屏及ViewWillAppear及视图与导航控制器嵌套时的不同反应
  12. Python 练习册,每天一个小程序----第0000题
  13. Netty学习路线
  14. Eclipse 添加 lib (导入 .jar 包)
  15. req和resp常用的方法
  16. 【Windows 7】发现一个奇怪的现象
  17. bootstrap-treeview 如何实现全选父节点下所有子节点及反选
  18. 关于Jedis是否线程安全的测试
  19. Pycharm学习python路
  20. DateGridView标题列头添加复选框

热门文章

  1. make[1]: *** [storage/perfschema/unittest/CMakeFiles/pfs_connect_attr-t.dir/all] 错误 2 解决方法
  2. 天兔(Lepus)数据库监控系统快速安装部署
  3. 如何保证修改resolv.conf后重启不恢复?
  4. XML格式化工具
  5. Jupyter/JupyterLab安装使用
  6. [Java并发编程(一)] 线程池 FixedThreadPool vs CachedThreadPool ...
  7. 大批量delete 优化方案
  8. C# SMTP 邮件发送之QQ邮箱篇
  9. acl && prefix list
  10. minicom for Mac 配置