#Leetcode# 788. Rotated Digits
https://leetcode.com/problems/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].
代码:
class Solution {
public:
int rotatedDigits(int N) {
int ans = 0;
for(int i = 1; i <= N; i ++) {
if(isgood(i)) ans ++;
}
return ans;
}
bool isgood(int x) {
string s = to_string(x);
bool flag = false;
for(int i = 0; i < s.length(); i ++) {
if(s[i] == '3' || s[i] == '7' || s[i] == '4') return false;
if(s[i] == '2' || s[i] == '5' || s[i] == '6' || s[i] == '9') flag = true;
}
return flag;
}
};
如果数字里出现 3 4 7 数字的话就不是好数字 逐位判断 还是 emmmm 阔以的吧
#Leetcode# 788. Rotated Digits的更多相关文章
- 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 (旋转数字)
X is a good number if after rotating each digit individually by 180 degrees, we get a valid number t ...
- 【Leetcode_easy】788. Rotated Digits
problem 788. Rotated Digits solution1: class Solution { public: int rotatedDigits(int N) { ; ; i< ...
- [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】788. Rotated Digits 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 788. 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_Easy tag: **Dynamic Programming
基本思路建一个helper function, 然后从1-N依次判断是否为good number, 注意判断条件为没有3,4,7 的数字,并且至少有一个2,5,6,9, 否则的话数字就一样了, 比如8 ...
- LeetCode 788. 旋转数字(Rotated Digits) 36
788. 旋转数字 788. Rotated Digits 题目描述 我们称一个数 X 为好数, 如果它的每位数字逐个地被旋转 180 度后,我们仍可以得到一个有效的,且和 X 不同的数.要求每位数字 ...
随机推荐
- ES5-ES6-ES7_const声明只读常量
const 概述const声明一个只读的常量.一旦声明,常量的值就不能改变.const声明的变量不得改变值,这意味着,const一旦声明变量,就必须立即初始化,不能留到以后赋值. // const P ...
- Django之ORM查询复习与cookie
ORM查询总结: models.Book.objects.filter(**kwargs): querySet [obj1,obj2] models.Book.objects.filter(**kwa ...
- 【洛谷】【动态规划/01背包】P1734 最大约数和
[题目描述:] 选取和不超过S的若干个不同的正整数,使得所有数的约数(不含它本身)之和最大. [输入格式:] 输入一个正整数S. [输出格式:] 输出最大的约数之和. [算法分析:] 01背包,每个数 ...
- yii2查询汇总
页面输出list信息 return $this->render('index', [ 'model' => $model, 'cats' => $cats, 'findlist' = ...
- 获取键盘的ascii码
waitKey(1) & 0xFF获取当前按的键的ascii码,
- 国产的骄傲,Deepin发布v15.9
深度操作系统是一个致力于为全球用户提供美观易用.安全可靠的Linux发行版.深度操作系统基于Linux内核,以桌面应用为主的开源GNU/Linux操作系统,支持笔记本.台式机和一体机.深度操作系统(d ...
- ES5与ES6对比
ES5与ES6对比 1. 模块引用 1.在ES5里,引入React包基本通过require进行,代码类似这样: // ES5 var React = require('react'); var { C ...
- PAT A1139 First Contact (30 分)——set
Unlike in nowadays, the way that boys and girls expressing their feelings of love was quite subtle i ...
- luogu P2706 巧克力
题目 (第一道绿题) 有点像最大子矩阵qwq 用前缀和存图,l,r代表横向的一段区间,区间和就是a[r]-a[l-1] 然后用一个k从上到下dp...因为每次l,r变化的时候原来的k就没有用了,所以k ...
- java中使用jxl读取excel中的数据
package bboss; import java.io.File; import java.io.FileInputStream; import java.io.IOException; impo ...