作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/rotated-digits/description/

题目描述

X is a good number if after rotating each digit individually by 180 degrees, we get a valid number that is different from X. 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.

Now given a positive number N, how many numbers X from 1 to N are good?

Example:
Input: 10
Output: 4
Explanation:
There are four good numbers in the range [1, 10] : 2, 5, 6, 9.
Note that 1 and 10 are not good numbers, since they remain unchanged after rotating.

Note:

  1. N will be in range [1, 10000].

题目大意

在[1,N]双闭区间中,有多少个数字,将其倒影之后和自身不同。

解题方法

重要的是理解题意,就好比下面的这个倒影,要求倒影和自身不同,但倒影也必须是数字:

可以总结出以下的要求:

  1. 该数字中不含[3, 4, 7],否则其倒影不是数字。
  2. 该数字中必须包含[2, 5, 6, 9]中的至少一个,否则倒影和原数字相同

最后的结果是有多少个,遍历之后很容易得到答案。

class Solution(object):
def rotatedDigits(self, N):
"""
:type N: int
:rtype: int
"""
valid = [2, 5, 6, 9]
nonValid = [3, 4, 7]
def isGood(num):
for y in nonValid:
if str(y) in str(num):
return False
return any(str(x) in str(num) for x in valid)
return sum(map(int, [isGood(n) for n in range(1, N + 1)]))

二刷,基于同样的思想,写了一个更简洁的代码。

class Solution(object):
def rotatedDigits(self, N):
"""
:type N: int
:rtype: int
"""
dmap = {"0" : "0", "1" : "1", "8" : "8", "2" : "5", "5" : "2", "6" : "9", "9" : "6"}
res = 0
for num in range(1, N + 1):
numlist = list(str(num))
if any(x in numlist for x in ["3", "4", "7"]):
continue
numRotate = map(lambda x : dmap[x], numlist)
if numRotate == numlist:
continue
res += 1
return res

看了别人的提交,发现了一个更简单的思路,就是我们不需要把翻转后的数字构建出来,我们只需要找出特定的字符是否在字符串中即可。比如,如果数字包含["3", "4", "7"],那么肯定不可以。如果数字包含["2", "5", "6", "9"],那么一定可以。如果这些数字都不包含,那么就是翻转之后是自身的数字,就不能计算到结果里。

class Solution(object):
def rotatedDigits(self, N):
"""
:type N: int
:rtype: int
"""
dmap = {"0" : "0", "1" : "1", "8" : "8", "2" : "5", "5" : "2", "6" : "9", "9" : "6"}
res = 0
for num in range(1, N + 1):
if any(x in str(num) for x in ["3", "4", "7"]):
continue
if any(x in str(num) for x in ["2", "5", "6", "9"]):
res += 1
return res

日期

2018 年 2 月 26 日
2018 年 11 月 11 日 —— 剁手节快乐

最新文章

  1. jQuery初始化加载的实现
  2. 金蝶K/3 Cloud 界面解析过程
  3. SQLServer修改字段类型
  4. llinux 压缩 解压
  5. iOS开发UI篇—直接使用UITableView Controller
  6. Codeforces Round #313 (Div. 1) A. Gerald's Hexagon 数学题
  7. GPRS组网的几种方案【来自网络】
  8. 调试环境部署续:vs远程调试
  9. cocos2D(二)---- cocos2D文档的使用
  10. 使用WindowManager添加您自己的自定义视图
  11. CCNA网络工程师学习进程(9)GNS3的安装与配置
  12. gunicorn 简介
  13. js中时间的处理
  14. Java 领域从传统行业向互联网转型你必须知道的事儿
  15. upload.go
  16. Django对于模型的数据操作
  17. 剑指Offer编程题2——替换空格
  18. MongoDB数据查询 --MongoDB
  19. MUI 自定义从底部弹出的弹出框
  20. CSS3 根据屏幕大小显示内容(@media)

热门文章

  1. C语言 自定义函数按行读入文件
  2. tcp可靠传输的机制有哪些(面试必看
  3. Vue 前端配置多级目录实践(基于Nginx配置方式)
  4. stm32串行设备接口SPI控制max31865
  5. 【leetcode】834. Sum of Distances in Tree(图算法)
  6. MyBatis常用批量方法
  7. springmvc中的异常处理方法
  8. 【力扣】188. 买卖股票的最佳时机 IV
  9. 如何简单的理解LSTM——其实没有那么复杂(转载)
  10. windows 显示引用账户已被锁定,且可能无法登录