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
LeetCode 788 Rotated Digits 解题报告的更多相关文章
- 【LeetCode】788. Rotated Digits 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- #Leetcode# 788. Rotated Digits
https://leetcode.com/problems/rotated-digits/ X is a good number if after rotating each digit indivi ...
- 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 258 Add Digits 解题报告
题目要求 Given a non-negative integer num, repeatedly add all its digits until the result has only one d ...
- 【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 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 ...
- 【Leetcode_easy】788. Rotated Digits
problem 788. Rotated Digits solution1: class Solution { public: int rotatedDigits(int N) { ; ; i< ...
随机推荐
- 【20180409】IT管理之IT十二条令
团队越来越大,靠人管几乎有力无心,只能靠制度管理了. 前段时间对部门颁布了12条令,效果明显. 特此Mark. 汇报: 三条总结:汇报讲究精简,一个事情最多一句话概括. 一页报告:内容精简,报告一页w ...
- Introducing Apache Spark Datasets(中英双语)
文章标题 Introducing Apache Spark Datasets 作者介绍 Michael Armbrust, Wenchen Fan, Reynold Xin and Matei Zah ...
- Java8新特性interface中的static方法和default方法
static方法 java8中为接口新增了一项功能:定义一个或者更多个静态方法.用法和普通的static方法一样. 代码示例 public interface InterfaceA { /** * 静 ...
- AICODER全栈实习报名
三期班开始报名 三期班定于6月17日开班,实习费用如下: 三个月模式实习费为12000元. 一个月模式实习费为4500元. AICODER提供后付费模式,报名参加AICODER的线下实习3个月模式的朋 ...
- HTTP 03 HTTP 报文
客户端的HTTP报文, 叫做请求报文 服务器端的叫做 响应报文. HTTP 报文本身是由多行 (用 CR+LF 作换行符) 数据构成的字符串文本. HTTP 报文大致分为报文首部 和 报文主体 两部分 ...
- r里面如何实现两列数据合并为一列
library(dplyr) unite(mtcars, "vs_am", vs, am) Merging Data Adding Columns To merge two dat ...
- 【转】Cowboy 开源 WebSocket 网络库
原文链接: http://www.cnblogs.com/gaochundong/p/cowboy_websockets.html
- 使用docker安装mysql服务
使用docker官方镜像安装mysql服务 1 拉取mysql镜像,采用网易加速地址 docker pull hub.c.163.com/library/mysql:5.7 2 重命名镜像名 dock ...
- nginx default_server的作用
用来处理没有成功匹配server_name的请求 https://www.oschina.net/question/12_3565 https://segmentfault.com/a/1190000 ...
- 【Dubbo 源码解析】06_Dubbo 服务调用
Dubbo 服务调用 根据上图,可以看出,服务调用过程为: Consumer 端的 Proxy 调用 Cluster 层选择集群中的某一个 Invoker(负载均衡) Invoker 最终会调用 Pr ...