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 ...
随机推荐
- 写js过程中遇到的一个bug
<div class="func_Div" id="xxcx"><span>信息查询</span> ...
- VS 快捷键和正则替换
本文在VS2017中可用 1.注释 :Ctrl K C 取消注释: Ctrl K U 2.整理代码格式: Ctrl K D 或者 Ctrl K F 3.快速切换不同的代码窗口 Ctrl+Tab 4 ...
- 一维、二维数组 与 常用的返回数组 以及 fetch_all与fetch_row的区别
一维数组:单行单列的数组. 二维数组:多行多列的数组. (至少两行两列) 索引数组: fetch_all() 返回所有数组 fetch_row() 返回一行或一列数组 (第二行需要输入两 ...
- CentOS6.5下源码安装MySQL5.6.35
接上一篇文章使用RPM包安装MySQL,确实很方便.但是安装后却不知道各文件保存在哪个文件夹下!尝试使用源码安装~本文主要参考:CentOS 6.4下编译安装MySQL 5.6.14一.卸载旧版本 . ...
- Vuejs实战项目五:数据列表
1.在EasyMock 中添加数据列表模拟接口 请求url:/suyuan/list 请求方式:get 描述:数据列表 mock.js配置: 例: { "code": 2000, ...
- TZOJ 5963 Increasing Sequences(线性DP)
描述 Given a string of digits, insert commas to create a sequence of strictly increasing numbers so as ...
- HDU 5266 pog loves szh III 线段树,lca
Pog and Szh are playing games. Firstly Pog draw a tree on the paper. Here we define 1 as the root of ...
- css3正方体效果
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 没有ORM库的时候,通过PDO连接MySQL的方法
$pdo = new PDO("mysql:host=localhost;dbname=eq","root","root"); $pdo-& ...
- Iterm2 快捷键介绍
Mac 原来自带的终端工具 Terminal 不好用是出了名的,虽然最近几个版本苹果稍微做了些优化,功能上,可用性方面增强不少,无奈有个更好用的 Iterm2 摆在那,基本上也就没有多少出场机会了 I ...