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?

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

Note:

  • N  will be in range [1, 10000].
  1. class Solution(object):
  2. def rotatedDigits(self, N):
  3. """
  4. :type N: int
  5. :rtype: int
  6. """
  7. ans=0
  8.  
  9. for i in range(N):
  10. l=str(i+1)
  11. if '3' not in l and '4' not in l and '7' not in l:
  12. if '1' in l or '0' in l or '8' in l:
  13. if '2' in l or '5' in l or '6' in l or '9' in l:
  14. ans+=1
  15. else:
  16. ans+=1
  17.  
  18. return ans

  

[LeetCode&Python] Problem 788. Rotated Digits的更多相关文章

  1. [LeetCode&Python] Problem 258. Add Digits

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

  2. 【Leetcode_easy】788. Rotated Digits

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

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

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

  4. LeetCode 788 Rotated Digits 解题报告

    题目要求 X is a good number if after rotating each digit individually by 180 degrees, we get a valid num ...

  5. #Leetcode# 788. Rotated Digits

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

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

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

  7. 788. Rotated Digits

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

  8. [LeetCode&Python] Problem 415. Add Strings

    Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...

  9. [LeetCode&Python] Problem 202. Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

随机推荐

  1. 从使用角度看 ReentrantLock 和 Condition

    java 语言中谈到锁,少不了比较一番 synchronized 和 ReentrantLock 的原理,本文不作分析,只是简单介绍一下 ReentrantLock 的用法,从使用中推测其内部的一些原 ...

  2. [LeetCode] Network Delay Time 网络延迟时间——最短路算法 Bellman-Ford(DP) 和 dijkstra(本质上就是BFS的迭代变种)

    There are N network nodes, labelled 1 to N. Given times, a list of travel times as directed edges ti ...

  3. Apache升级PHP教程(以5.3.3升级到5.6.30为例)

    最简单的LAMP环境搭建当然是通过yum来安装,但由于镜像仓库中的软件版本更新较慢,经常会遇到版本过旧的问题,尤其是安装一些新版本的CMS时的PHP. 这时我们需要手动编译PHP,Linux编译安装经 ...

  4. js如何返回两个数的商的整数和余数部分?

    js中,如何返回两个数的商的整数和余数部分? 如: num1:100 ,num2:12 var num = parseFloat(100/12); //值: 8.333333333333334 那么如 ...

  5. dl简单模板,无pretraining过程

    layer_dimensions = [11 22 33 22 11]'; ld_size = size(layer_dimensions , 1); % what is deal [x rx dx ...

  6. 《Java面向对象编程》

    <Java面向对象编程> 第11章 对象的生命周期 11.1  创建对象的方式 用new语句创建对象 运用反射手段,调用java.lang.Class 或者 java.lang.Const ...

  7. 【资料收集】Converting Between cv::Mat and QImage or QPixmap

    参考: 方法一 Convert between cv::Mat and QImage 两种图片类转换 - Grandyang - 博客园 http://www.cnblogs.com/grandyan ...

  8. python ----字符串基础练习题30道

    1.执行python脚本的两种方式 一种是点开始--运行--cmd 方式(这个操作需要先配置好环境变量path路径)之后运行python 二是直接进安装目录 运行tython软件运行.pycharm ...

  9. DevExpress v18.1新版亮点——Reporting篇(三)

    用户界面套包DevExpress v18.1日前终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress Reporting v18.1 的新功能,快来下载试用新版本 ...

  10. 2017 秦皇岛CCPC Balloon Robot (ZOJ 3981)

    题意:给出n个队伍,m个座位,q次A题的队伍与时间,下一行是n个队伍的坐的位置,再下面q行就是第x个队再第y秒A出一道题,然后有一个机器人,开始位置由你选,他每走一步 他就会向右走一格,走到m的时候会 ...