#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 不同的数.要求每位数字 ...
随机推荐
- swift class的缺省基类(SwiftObject)与内存模型
Hard Constraints on Resilience The root of a class hierarchy must remain stable, at pain of invalida ...
- CF1045G:AI robots(CDQ分治)
Description 火星上有$n$个机器人排成一行,第$i$个机器人的位置为$x_i$,视野为$r_i$,智商为$q_i$.我们认为第$i$个机器人可以看到的位置是$[x_i−r_i,x_i+ ...
- c++函数库中一些实用的函数
有一些程序,虽然写起来不难,但是可能比较麻烦或容易出错,这时就可以用c++函数库里自带的一些实用的函数. 这里只记录一些不太常见的函数. ------------------------------- ...
- 2018 湖南网络比赛题 HDU - 6286 (容斥)
题意:不说了. 更加偏向于数学不好的小可爱来理解的. 这篇博客更加偏重于容斥的讲解.用最直观的数学方法介绍这个题. 思路: 在a<=x<=b. c<=y<=d 中满足 x*y ...
- MSSQL Server 数据库备份还原常用SQL语句及注意
1.备份数据库 backup database db_name to disk='d:\db_name.bak' with format --通过使用with format可以做到覆盖任何现有的备份和 ...
- Arduino IDE for ESP8266 项目云盒子 (1)AP直接模式
手机直接连接esp8266辐射的WIFI,通信. https://item.taobao.com/item.htm?spm=a230r.1.14.20.eYblO3&id=5219451024 ...
- Solaris 11配置IPS安装系统包(类似linux中的yum源)
参考:http://blog.chinaunix.net/uid-8860-id-3777457.html 一. 概述: Solaris 11被称为第一个云操作系统,因此在很多方面体现了云系统的一些特 ...
- MATLAB:控制系统模型变换
1.多项式转换为零极点 [z,p,k]=tf2zp(num,den); 2.零极点转换为多项式 [num,den]=zp2tf(z,p,k); 3.状态空间转换成多项式传递函数 [num,den]=s ...
- iOS更新惹怒高通:苹果太可耻!
之前高通同时在德国.中国发起对苹果的专利诉讼,而他们都赢得了最终的胜利,其中包含iPhone 7.8以及X系列机型,统统在禁售机型当中. 从法院公布的细节看,高通对iPhone禁售的理由是,iOS系统 ...
- AMD和CMD规范
1.名词解释AMD:Asynchronous Modules Definition异步模块定义,提供定义模块及异步加载该模块依赖的机制.CMD:Common Module Definition 通用模 ...