题目要求

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 解题报告的更多相关文章

  1. 【LeetCode】788. Rotated Digits 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. #Leetcode# 788. Rotated Digits

    https://leetcode.com/problems/rotated-digits/ X is a good number if after rotating each digit indivi ...

  3. LeetCode 788. Rotated Digits (旋转数字)

    X is a good number if after rotating each digit individually by 180 degrees, we get a valid number t ...

  4. LeetCode 258 Add Digits 解题报告

    题目要求 Given a non-negative integer num, repeatedly add all its digits until the result has only one d ...

  5. 【LeetCode】738. Monotone Increasing Digits 解题报告(Python)

    [LeetCode]738. Monotone Increasing Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...

  6. 【LeetCode】402. Remove K Digits 解题报告(Python)

    [LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  7. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  8. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  9. 【Leetcode_easy】788. Rotated Digits

    problem 788. Rotated Digits solution1: class Solution { public: int rotatedDigits(int N) { ; ; i< ...

随机推荐

  1. window下zookeeper的下载启动和报错等问题

    在使用dubbo等需要用到zookeeper,之前window下本地部署,启动一直有问题,后面折腾了下才部署成功,此次记录下来. 将zookeeper下载之后,解压到指定目录即可,无需安装.例如:解压 ...

  2. C#设计模式 ---- 总结汇总

    一.引言 C#版本的23种设计模式已经写完了,现在也到了一个该总结的时候了.说起设计模式,我的话就比较多了.刚开始写代码的时候,有需求就写代码来解决需求,如果有新的需求,或者需求变了,我就想当然的修改 ...

  3. 存货控制中的ABC分类释义

    存货控制的ABC制度是根据存货的重要程度把存货归为A.B.C三类,最重要的是A类,最不重要的是C类. A类产品就是指在产品销售进程中,销量比较多,在库存管理方面需要大量备货的产品; B类则是销量适中, ...

  4. python和C++联合调试

    python可以利用SO的方式去调用C++中的函数,但是需要一种调试方案来进行python和C++的联合调试,效果是直接在c++代码中打断点,然后python在进行c++so调用的时候,直接进入到断点 ...

  5. Android中的指纹识别

    转载请注明出处:http://blog.csdn.net/wl9739/article/details/52444671 评论中非常多朋友反映,依据我给出的方案,拿不到指纹信息这个问题,在这里统一说明 ...

  6. [Linux]systemd和sysV

    转自:https://www.cnblogs.com/EasonJim/p/7168216.html 在Debian8中systemd和sysVinit同时存在,NTP就是在/etc/init.d/n ...

  7. stm32f0 学习

    http://blog.csdn.net/mrlixirong/article/category/5842873

  8. iframe子页面与父页面元素的访问以及js变量的访问[zhuan]

    https://www.cnblogs.com/Capricorn-HCL/articles/4216302.html

  9. linux php --ini

    $ php --ini

  10. ubuntu如何安装或更换内核

    内核是一个系统的灵魂,系统在启动的时候,就是基于相关的内核启动该系统的.我们怎么样更改ubuntu系统的内核并运行它呢? ubuntu18.04LTS 互联网安装内核. 安装内核的步骤非常简单,我们可 ...