Leetcode788.Rotated Digits旋转数字
我们称一个数 X 为好数, 如果它的每位数字逐个地被旋转 180 度后,我们仍可以得到一个有效的,且和 X 不同的数。要求每位数字都要被旋转。
如果一个数的每位数字被旋转以后仍然还是一个数字, 则这个数是有效的。0, 1, 和 8 被旋转后仍然是它们自己;2 和 5 可以互相旋转成对方;6 和 9 同理,除了这些以外其他的数字旋转以后都不再是有效的数字。
现在我们有一个正整数 N, 计算从 1 到 N 中有多少个数 X 是好数?
示例: 输入: 10 输出: 4 解释: 在[1, 10]中有四个好数: 2, 5, 6, 9。 注意 1 和 10 不是好数, 因为他们在旋转之后不变。
注意:
- N 的取值范围是 [1, 10000]。
class Solution {
public:
int rotatedDigits(int N) {
int res = 0;
for(int i = 1; i<= N; i++)
{
if(Check(i))
{
res++;
}
}
return res;
}
bool Check(int x)
{
int temp = x;
vector<int> save;
while(temp)
{
int m = temp % 10;
if(m == 3 || m == 4 || m == 7)
return false;
save.push_back(m);
temp /= 10;
}
int cnt = 1;
for(int i = 0; i < save.size(); i++)
{
if(save[i] == 1 || save[i] == 0 || save[i] == 8)
temp = temp + save[i] * cnt;
else if(save[i] == 2)
temp = temp + 5 * cnt;
else if(save[i] == 5)
temp = temp + 2 * cnt;
else if(save[i] == 6)
temp = temp + 9 * cnt;
else if(save[i] == 9)
temp = temp + 6 * cnt;
cnt *= 10;
}
if(temp == x)
return false;
return true;
}
};
Leetcode788.Rotated Digits旋转数字的更多相关文章
- LeetCode 788. Rotated Digits (旋转数字)
X is a good number if after rotating each digit individually by 180 degrees, we get a valid number t ...
- [LeetCode] Rotated Digits 旋转数字
X is a good number if after rotating each digit individually by 180 degrees, we get a valid number t ...
- 788. Rotated Digits 旋转数字
[抄题]: X is a good number if after rotating each digit individually by 180 degrees, we get a valid nu ...
- LeetCode 788. 旋转数字(Rotated Digits) 36
788. 旋转数字 788. Rotated Digits 题目描述 我们称一个数 X 为好数, 如果它的每位数字逐个地被旋转 180 度后,我们仍可以得到一个有效的,且和 X 不同的数.要求每位数字 ...
- 【Leetcode_easy】788. Rotated Digits
problem 788. Rotated Digits solution1: class Solution { public: int rotatedDigits(int N) { ; ; i< ...
- Java实现 LeetCode 788 旋转数字(暴力)
788. 旋转数字 我们称一个数 X 为好数, 如果它的每位数字逐个地被旋转 180 度后,我们仍可以得到一个有效的,且和 X 不同的数.要求每位数字都要被旋转. 如果一个数的每位数字被旋转以后仍然还 ...
- [Swift]LeetCode788. 旋转数字 | Rotated Digits
X is a good number if after rotating each digit individually by 180 degrees, we get a valid number t ...
- C#LeetCode刷题之#788-旋转数字(Rotated Digits)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3967 访问. 我们称一个数 X 为好数, 如果它的每位数字逐个地 ...
- LeetCode 788 Rotated Digits 解题报告
题目要求 X is a good number if after rotating each digit individually by 180 degrees, we get a valid num ...
随机推荐
- Go语言cookie的基本操作
cookie数据结构介绍 cookie数据结构介绍 a. Expires,cookie过期时间,使用绝对时间.比如2018/10/10 10:10:10b. MaxAge,cookie过期时间,使用相 ...
- jsp导出的word默认打开是web视图,希望是页面视图
方法1 ( velocity+java )我也遇到了这个问题,已经解决:1 .<html xmlns:v='urn:schemas-microsoft-com:vml'xmlns:o='urn: ...
- CSS3画图
众所周知,用CSS3的圆角.转换可以画出各种不同的形状,制作不同的图案,早些前先驱者已画出经典的叮当猫,iphone手机等展示CSS3的强大实力,趁最近有空我也对CSS3进行了一些实践,颇有收获. 用 ...
- TZOJ 1503 Incredible Cows(折半搜索+二分)
描述 Farmer John is well known for his great cows. Recently, the cows have decided to participate in t ...
- Leetcode162. Find Peak Element寻找峰值
示例 2: 输入: nums = [1,2,1,3,5,6,4] 输出: 1 或 5 解释: 你的函数可以返回索引 1,其峰值元素为 2: 或者返回索引 5, 其峰值元素为 6. 说明: 你的解法 ...
- Redis功能类
<?phpnamespace org; /** * redis操作类 * 说明,任何为false的串,存在redis中都是空串. * 只有在key不存在时,才会返回false. * 这点可用于防 ...
- 利用Nginx轻松实现Ajax的跨域请求(前后端分离开发调试必备神技)
利用Nginx轻松实现浏览器中Ajax的跨域请求(前后端分离开发调试必备神技) 前言 为什么会出现跨域? 造成跨域问题的原因是因为浏览器受到同源策略的限制,也就是说js只能访问和操作自己域下的资源,不 ...
- PHP--时间格式处理
Ymd格式转Y-m-d或转成时间戳将Ymd格式如19930811转成1993-08-11格式 date('Y-m-d',strtotime('19930811') 将Ymd格式如19930811转成时 ...
- c#还有一点不太明白,既然开启了线程为何还要委托呢?
2013-07-15 09:33threadroc | 浏览 1611 次 既然开启了线程为何还要委托呢?,开启线程本身不就是委托吗?为何委托要显示声明呢?Thread thread = new Th ...
- linux 下 nginx 的最大文件打开设置
先设置好linux 系统的 limit, 见其他博文 nginx 也有自己的 limit 设置: 1. nginx.conf 设置 worker_rlimit_nofile 例如: worker_ ...