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< ...
随机推荐
- sql中的left join以及on、where关键字的区别
创建两张表并插入一些数据 create table class( class_id int, class_name ), class_grade ) ); ,'语文','A'); ,'数学','B') ...
- 飞思卡尔单片机P&E开发工具硬件及软件
原文链接: http://blog.sina.com.cn/s/blog_8ebff8d7010121tm.html 1.HC(S)08系列 开发机硬件:USB-ML-12 CYCLONE PRO U ...
- fastcgi php-cgi与php-fpm区别和之间的关系
关于FastCGI.php-cgi.php-fpm的区别是什么,各自有什么用途,以及相互间的关系是什么,查阅相关资料,可谓是众说纷纭,莫衷一是: 说法一:fastcgi是一个协议,php-fpm实现了 ...
- Android Api 27 在 Android 8.0 上出现 Only fullscreen opaque activities can request orientation 的解决情况
刚上班,没有业务开发,对 App 的 Api 由 26 升级到了 27, 结果在 Android 8.0 的设备上会出现 crash . Log 如下: java java.lang.IllegalS ...
- android: android 中的ColorMatrix (转)
Android中有两个比较重要的矩阵,ColorMatrix和Matrix.ColorMatrix用来改变bitmap的颜色和透明度,Matrix用来对bitmap平移.缩放.错切.对矩阵的概念不理解 ...
- word,excel,ppt,txt转换为 PDF
/// <summary> /// 将word文档转换成PDF格式 /// </summary> /// <param name="sourcePath&quo ...
- MPU6050带字符驱动的i2c从设备驱动2
#include <linux/kernel.h> #include <linux/module.h> #include <linux/init.h> #inclu ...
- 查看和修改 mysql库、表、字段编码
查看编码 查看数据库编码. show variables like '%char%'; 查看表编码 show create table <表名>; 例:show create table ...
- [Z] 从Uncaught SyntaxError: Unexpected token ")" 问题看javascript:void的作用
https://blog.csdn.net/hongweigg/article/details/78094338 问题 在前端编程中,突然出现Uncaught SyntaxError: Unex ...
- there was an error running the selected code generator unable to retrieve metadata for
there was an error running the selected code generator unable to retrieve metadata for PROBLEM: I ha ...