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?
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 (旋转数字)的更多相关文章
- 788. Rotated Digits 旋转数字
[抄题]: X is a good number if after rotating each digit individually by 180 degrees, we get a valid nu ...
- [LeetCode] Rotated Digits 旋转数字
X is a good number if after rotating each digit individually by 180 degrees, we get a valid number t ...
- LeetCode 788 Rotated Digits 解题报告
题目要求 X is a good number if after rotating each digit individually by 180 degrees, we get a valid num ...
- #Leetcode# 788. Rotated Digits
https://leetcode.com/problems/rotated-digits/ X is a good number if after rotating each digit indivi ...
- Leetcode788.Rotated Digits旋转数字
我们称一个数 X 为好数, 如果它的每位数字逐个地被旋转 180 度后,我们仍可以得到一个有效的,且和 X 不同的数.要求每位数字都要被旋转. 如果一个数的每位数字被旋转以后仍然还是一个数字, 则这个 ...
- 【Leetcode_easy】788. Rotated Digits
problem 788. Rotated Digits solution1: class Solution { public: int rotatedDigits(int N) { ; ; i< ...
- 【LeetCode】788. Rotated Digits 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [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 ...
- LeetCode 258 Add Digits(数字相加,数字根)
翻译 给定一个非负整型数字,反复相加其全部的数字直到最后的结果仅仅有一位数. 比如: 给定sum = 38,这个过程就像是:3 + 8 = 11.1 + 1 = 2.由于2仅仅有一位数.所以返回它. ...
随机推荐
- 5步上手体验kettle快捷调度方式
https://my.oschina.net/u/944575/blog/1557410 kettle调度监控最佳实践 https://my.oschina.net/u/1026947/blog/15 ...
- Linux系统命令及文件的浏览、管理和维护
在linux中什么是一个文件的路径呢,说白了就是这个文件存在的地方,例如在上一章提到的/root/.ssh/authorized_keys 这就是一个文件的路径.如果你告诉系统这个文件的路径,那么系统 ...
- UML实例教程 解析UML建模分析与设计
UML统一建模语言在软件开发过程中非常实用,UMl建模的分析与设计你是否熟悉,这里就通过实例向大家介绍,希望通过本文的学习,你对UML建模的分析与设计方法有一定的了解. 本节向大家介绍一下图书管理系统 ...
- POJ_1050_(dp)
To the Max Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 48232 Accepted: 25534 Desc ...
- day21-2 类的派生
目录 类的派生 派生方法一 派生方法二 类的派生 派生:子类中新定义属性的这个过程叫做派生 派生方法一 指明道姓访问某一个类的函数:该方法与继承无关 class People: def __init_ ...
- CAD设置系统变量(com接口VB语言)
主要用到函数说明: MxDrawXCustomFunction::Mx_SetSysVar 设置系统变量.详细说明如下: 参数 说明 CString sVarName 系统变量名 Value 需要设置 ...
- HDU多校Round 8
Solved:2 rank:141 D. Parentheses Matrix n,m有一个小于6的时候是一种构造方法 答案是n + (m - 2) / 2 (n > m) 都大于6的时候 可以 ...
- react 导航切换
<ul class="nav"> <li onClick={() => this.changeFontColor(0)} className={`${0 = ...
- [Algorithm] 6. Merge Two Sorted Arrays
Description Merge two given sorted integer array A and B into a new sorted integer array. Example A= ...
- 【汇总】java中数组的声明、初始化及遍历
java中数组用来存储固定大小的同类型元素 一维数组: 1.数组的声明: //声明一维数组,推荐用第一种 int[] a; int b[]; 2.数据的初始化:有三种初始化方式 (1).静态初始化 / ...