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仅仅有一位数.所以返回它. ...
随机推荐
- 安卓TV盒子常见问题以及解决方法
1.为什么requestfocus无效 原因:requestfocus不支持在Touch模式下的Focus; 解法方案:再加一个requestFocusFromTouch函数. 2.摄像头打开问题,调 ...
- bower——基本使用
基本概念 bower可以解决项目的静态文件依赖的问题 bower是用nodejs开发的,所以要现状nodejs 安装nodejs应用程序,网上自行下载 检验是否成功安装,打开电脑cmd,执行node ...
- sublime text3 =个人插件
1.sublime text3汉化插件安装. ctrl+shift+p → Package Control:Install Package → ChineseLocalization preferen ...
- CAD得到所有组名(网页版)
主要用到函数说明: _DMxDrawX::GetAllGroupName 得到所有组名. js代码实现如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...
- 梦想CAD控件COM接口光栅图处理
在CAD操作过程中,我们在设计绘图时,光栅图像也就是我们常说的图片,应用非常广泛,在CAD中可以直接插入光栅图像,并且可以对光栅图像进行裁剪.透明度调整等一些操作,在网页可以快速实现我们所需功能. 一 ...
- 15Oracle Database 索引
Oracle Database 索引 索引 索引的目的是加快查询速度,就像一本数据的目录一样.建立索引的原则:非常少的DML操作:经常出现在where语句中的字段 2.20.1.建立索引 l 对t_ ...
- css3文字渐变无效果的解决方案
现在css3越来月流行了,为了实现一些高大上的效果,我们会用一些渐变的特效,请看文字渐变的特效代码: .title { font-size: 60px; line-height: 80px; text ...
- fread快读+fwrite快速输出
定义数组 char buf[1<<23],*p1=buf,*p2=buf,obuf[1<<23],*O=obuf; 读入 #define getchar() (p1==p2&a ...
- Go:值类型、引用类型
值类型,变量存的就是值本身: in系列t.float系列.bool.string.数组和struct 引用类型,变量存的是一个地址,这是地址存的才是值本身: 指针.slice.map.chan.int ...
- python3.3+selenium
1.查看C:\Python33\Scripts下已经有了easy_install.exe; 2.从这里下载pip tar.gz,并解压到C盘,https://pypi.python.org/pypi/ ...