【LeetCode】788. Rotated Digits 解题报告(Python)
作者: 负雪明烛
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:
- N will be in range [1, 10000].
题目大意
在[1,N]双闭区间中,有多少个数字,将其倒影之后和自身不同。
解题方法
重要的是理解题意,就好比下面的这个倒影,要求倒影和自身不同,但倒影也必须是数字:
可以总结出以下的要求:
- 该数字中不含
[3, 4, 7]
,否则其倒影不是数字。 - 该数字中必须包含
[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 日 —— 剁手节快乐
【LeetCode】788. Rotated Digits 解题报告(Python)的更多相关文章
- LeetCode 788 Rotated Digits 解题报告
题目要求 X is a good number if after rotating each digit individually by 180 degrees, we get a valid num ...
- #Leetcode# 788. Rotated Digits
https://leetcode.com/problems/rotated-digits/ X is a good number if after rotating each digit indivi ...
- LeetCode 258 Add Digits 解题报告
题目要求 Given a non-negative integer num, repeatedly add all its digits until the result has only one d ...
- LeetCode 788. Rotated Digits (旋转数字)
X is a good number if after rotating each digit individually by 180 degrees, we get a valid number t ...
- 【LeetCode】738. Monotone Increasing Digits 解题报告(Python)
[LeetCode]738. Monotone Increasing Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...
- 【LeetCode】402. Remove K Digits 解题报告(Python)
[LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
随机推荐
- 网络爬虫-python-爬取天涯求职贴
使用urllib请求页面,使用BeautifulSoup解析页面,使用xlwt3写入Excel import urllib.request from bs4 import BeautifulSoup ...
- 『学了就忘』Linux文件系统管理 — 62、手动分配swap分区
目录 1.查看swap分区情况 2.手动修改swap分区 3.格式化swap分区 4.使用swap分区 5.配置swap分区开机之后自动挂载 1.查看swap分区情况 swap分区就相当于是内存的一个 ...
- MybatisPlus的CRUD及拓展
创建一个简单的MybatisPlus项目在上一篇博客:MybatisPlus入门程序 一.CRUD 1. select 1.1 查找全部用户 //查 @Test public void select( ...
- 学习Java的第十八天
一.今日收获 1.java完全学习手册第三章算法的3.1比较值 2.看哔哩哔哩上的教学视频 二.今日问题 1.在第一个最大值程序运行时经常报错. 2.哔哩哔哩教学视频的一些术语不太理解,还需要了解 三 ...
- words in English that contradict themselves
[S1E10, TBBT]Leonard: I don't get it. I already told her a lie. Why would I replace it with a differ ...
- day27 网络编程
1.OSI七层协议 1.七层划分为:应用层,表示层.会话层.传输层.网络层.数据链路层.物理层 2.五层划分:应用层.传输层.网络层.数据链路层.物理层 应用层: 表示层: 会话层: 传输层:四层交换 ...
- linux 6.5 网卡
启动网卡 ifup eth0 eth0:网卡名称 设置网卡开机启动 vi /etc/sysconfig/network-scripts/ifcfg-eth0 ONBOOT=yes
- cordova配置与开发
1.环境配置 1.1.安装ant 从 apache官网 下载ant,安装并配置,将ant.bat所在目录加到path环境变量,如c:\apache-ant\bin\.在cmd中运行以下语句如不报错即可 ...
- 【编程思想】【设计模式】【结构模式Structural】代理模式Proxy
Python版 https://github.com/faif/python-patterns/blob/master/structural/proxy.py #!/usr/bin/env pytho ...
- 【Java 基础】Java日期格式问题
1. Use SimpleDateFormat to format Date. Watch out, SDF is NOT THREAD-SAFE, it might not be important ...