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]

题目标签:String

  题目给了我们一个N, 让我们找出从 1 到 N 中有几个 good number。Good Number 是每一个digit 都可以旋转,然后旋转后 与 旋转前 是不同的 数字。

  利用HashMap 把 0,1,8,2,5,6,9 这些可以旋转的数字存入保存。

  之后利用Map来检查数字的每一个 digit 是否可以旋转,当旋转完之后的新数字 要与 旧数字 不同,才可以算。

  具体请看code。

  

  

Java Solution:

Runtime beats  23.35%

完成日期:05/11/2018

关键词:HashMap

关键点:把可以旋转的digit存入map

 class Solution
{
public int rotatedDigits(int N)
{
int count = 0;
HashMap<Integer, Integer> map = new HashMap<>(); map.put(0, 0);
map.put(1, 1);
map.put(8, 8);
map.put(2, 5);
map.put(5, 2);
map.put(6, 9);
map.put(9, 6); for(int i=1; i<=N; i++)
{
int number = i;
int newNumber = 0;
int m = 1; while(number > 0)
{
int digit = number % 10; // get the digit if(map.containsKey(digit)) // check if digit can be rotated
{
newNumber = map.get(digit) * m + newNumber;
number /= 10;
m *= 10;
}
else // if digit is invalid
{
break;
}
} if(number == 0 && i != newNumber)
count++; } return count;
}
}

参考资料:n/a

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

LeetCode 788. Rotated Digits (旋转数字)的更多相关文章

  1. 788. Rotated Digits 旋转数字

    [抄题]: X is a good number if after rotating each digit individually by 180 degrees, we get a valid nu ...

  2. [LeetCode] Rotated Digits 旋转数字

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

  3. LeetCode 788 Rotated Digits 解题报告

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

  4. #Leetcode# 788. Rotated Digits

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

  5. Leetcode788.Rotated Digits旋转数字

    我们称一个数 X 为好数, 如果它的每位数字逐个地被旋转 180 度后,我们仍可以得到一个有效的,且和 X 不同的数.要求每位数字都要被旋转. 如果一个数的每位数字被旋转以后仍然还是一个数字, 则这个 ...

  6. 【Leetcode_easy】788. Rotated Digits

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

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

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

  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. LeetCode 258 Add Digits(数字相加,数字根)

    翻译 给定一个非负整型数字,反复相加其全部的数字直到最后的结果仅仅有一位数. 比如: 给定sum = 38,这个过程就像是:3 + 8 = 11.1 + 1 = 2.由于2仅仅有一位数.所以返回它. ...

随机推荐

  1. Android显示相册图片和相机拍照

    首先看最重要的MainActive类: public class MainActivity extends AppCompatActivity { private final int FROM_ALB ...

  2. Angular——作用域

    基本介绍 应用App是无法嵌套的,但是controller是可以嵌套的,每个controller都会对应一个模型(model)也就是$scope对象,不同层级的controller下的$scope遍产 ...

  3. windows 下完全卸载service

    用SC Delete命令的话,如果服务名称中带空格,则请在服务名称前面用半角的双引号括起,如SC delete "Adobe LM Service",另外Services这个子键一 ...

  4. 《网络管理》IP地址管理与子网划分

    IP地址管理——ipmaster ipmaster是一款对IP地址进行管理的软件,使用该软件可以提高网络管理员的工作效率.在大型网络中,使用该软件可以有序且高效地实现大中小型企业网IP地址的分配和管理 ...

  5. 06Oracle Database 数据类型

    Oracle Database 数据类型 字符型 char(n)最大2000个字节 定长 nchar(n)最大2000个字节 变长 varchar2(n) 最大4000个字节 变长 nvarchar2 ...

  6. 链表相关的leetcode重要题目

    Leetcode 92:反转链表II 解决这道题需要三个步骤: 找到需要反转的第一个节点.可以通过头节点前进m-1步,找到反转开始的位置. 将需要反转的部分进行反转.参考Leetcode 206:反转 ...

  7. 荷兰国旗问题、快排以及BFPRT算法

    荷兰国旗问题 给定一个数组arr,和一个数num,请把小于num的数放数组的左边,等于num的数放在数组的中间,大于num的数放在数组的右边.要求额外空间复杂度O(1),时间复杂度O(N). 这个问题 ...

  8. react 导航切换

    <ul class="nav"> <li onClick={() => this.changeFontColor(0)} className={`${0 = ...

  9. Java笔记——String、StringBuffer和StringBuilder类

    String类是不可变类,即一旦一个String对象被创建以后,包含在这个对象中的字符串序列是不可改变的,直至这个对象被销毁.   StringBuffer对象则代表一个字符序列可变的字符串,当一个S ...

  10. Bet(The 2016 ACM-ICPC Asia China-Final Contest 思路题)

    题目: The Codejamon game is on fire! Fans across the world are predicting and betting on which team wi ...