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?

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].
 class Solution {
public int rotatedDigits(int N) {
int count = 0;
for (int i = 1; i <= N; i ++) {
if (isValid(i)) count ++;
}
return count;
} public boolean isValid(int N) {
/*
Valid if N contains ATLEAST ONE 2, 5, 6, 9
AND NO 3, 4 or 7s
*/
boolean validFound = false;
while (N > 0) {
if (N % 10 == 2) validFound = true;
if (N % 10 == 5) validFound = true;
if (N % 10 == 6) validFound = true;
if (N % 10 == 9) validFound = true;
if (N % 10 == 3) return false;
if (N % 10 == 4) return false;
if (N % 10 == 7) return false;
N = N / 10;
}
return validFound;
}
}

Leetcode: Rotated Digits的更多相关文章

  1. [LeetCode] Rotated Digits 旋转数字

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

  2. LeetCode 788. 旋转数字(Rotated Digits) 36

    788. 旋转数字 788. Rotated Digits 题目描述 我们称一个数 X 为好数, 如果它的每位数字逐个地被旋转 180 度后,我们仍可以得到一个有效的,且和 X 不同的数.要求每位数字 ...

  3. 【Leetcode_easy】788. Rotated Digits

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

  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. LeetCode算法题-Rotated Digits(Java实现)

    这是悦乐书的第316次更新,第337篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第185题(顺位题号是788).如果一个数字经过180度旋转后,变成了一个与原数字不同的 ...

  8. [LeetCode&Python] Problem 788. Rotated Digits

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

  9. C#LeetCode刷题之#788-旋转数字(Rotated Digits)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3967 访问. 我们称一个数 X 为好数, 如果它的每位数字逐个地 ...

随机推荐

  1. MVC模式:action、dao、model、service、util

    这就是一个典型的MVC: action:主要是Struts2,用来做跳转,比如jsp页面提交的表单就是进入到action里面,然后action再调用service里面的逻辑,最后返回到jsp响应请求. ...

  2. 【转】java.io.Closeable接口

    说到java.io.Closeable接口就避不开java.lang.AutoCloseable接口,因为在java版本7.0时引入了java.lang.AutoCloseable接口,同时java. ...

  3. css Grid布局

    CSS Grid 布局完全指南(图解 Grid 详细教程)https://www.html.cn/archives/8510#prop-grid-gap 5分钟学会 CSS Grid 布局https: ...

  4. python的isinstance()函数

    以下是使用isinstance()函数的实例: a = isinstance(a,int) # 结果返回 True isinstance(a,str) # 结果返回 False 即:第1个参数是第2个 ...

  5. Java - Annotation使用

    本文转载于(这个写的很好):https://www.cnblogs.com/be-forward-to-help-others/p/6846821.html Annotation Annotation ...

  6. [Kubernetes] Pod Health

    Kubernetes relies on Probes to determine the health of a Pod container. A Probe is a diagnostic perf ...

  7. [React] Use the React Effect Hook in Function Components

    Similar to the State Hook, the Effect Hook is “first-class” in React and handy for performing side e ...

  8. ES 调优查询亿级数据毫秒级返回!怎么做到的?--文件系统缓存

    一道面试题的引入: 如果面试的时候碰到这样一个面试题:ElasticSearch(以下简称ES) 在数据量很大的情况下(数十亿级别)如何提高查询效率? 这个问题说白了,就是看你有没有实际用过 ES,因 ...

  9. Ubuntu安装php7.0环境

    1.下载必须组件 sudo apt-get install libxml2-dev sudo apt-get install curl 参考文献:http://php.net/manual/zh/in ...

  10. tar归档压缩命令和zip归档 和7zip压缩命令;库文件归档ar命令

    第一.tar 归档 tar -c 创建归档文件包 tar -x 释放归档文件包 tar -t 查看归档文件包 tar -v 显示归档包操作过程信息 tar -f 指定归档文件名 案例1:归档 /hom ...