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的更多相关文章

  1. LeetCode 788 Rotated Digits 解题报告

    题目要求 X is a good number if after rotating each digit individually by 180 degrees, we get a valid num ...

  2. LeetCode 788. Rotated Digits (旋转数字)

    X is a good number if after rotating each digit individually by 180 degrees, we get a valid number t ...

  3. 【Leetcode_easy】788. Rotated Digits

    problem 788. Rotated Digits solution1: class Solution { public: int rotatedDigits(int N) { ; ; i< ...

  4. [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 ...

  5. 【LeetCode】788. Rotated Digits 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. 788. Rotated Digits

    X is a good number if after rotating each digit individually by 180 degrees, we get a valid number t ...

  7. 788. Rotated Digits 旋转数字

    [抄题]: X is a good number if after rotating each digit individually by 180 degrees, we get a valid nu ...

  8. [LeetCode] 788. Rotated Digits_Easy tag: **Dynamic Programming

    基本思路建一个helper function, 然后从1-N依次判断是否为good number, 注意判断条件为没有3,4,7 的数字,并且至少有一个2,5,6,9, 否则的话数字就一样了, 比如8 ...

  9. LeetCode 788. 旋转数字(Rotated Digits) 36

    788. 旋转数字 788. Rotated Digits 题目描述 我们称一个数 X 为好数, 如果它的每位数字逐个地被旋转 180 度后,我们仍可以得到一个有效的,且和 X 不同的数.要求每位数字 ...

随机推荐

  1. Redis String类型的API使用

    package com.daxin.jedis_datastructure; import org.junit.After; import org.junit.Before; import org.j ...

  2. http请求的全过程

    参考资料 http://blog.jobbole.com/106632/ https://www.cnblogs.com/engeng/articles/5959335.html https://ww ...

  3. Spring容器IOC解析及简单实现(转)

    文章转自http://blog.csdn.net/liushuijinger/article/details/35978965

  4. 【转】利用python的KMeans和PCA包实现聚类算法

    转自:https://www.cnblogs.com/yjd_hycf_space/p/7094005.html 题目: 通过给出的驾驶员行为数据(trip.csv),对驾驶员不同时段的驾驶类型进行聚 ...

  5. labellmg使用方法

    https://www.cnblogs.com/Terrypython/p/9577657.html

  6. vue 实现tab切换动态加载不同的组件

    vue 实现tab切换动态加载不同的组件 使用vue中的is特性来加载不同的组件.具体看如下代码:这个功能对于vue比较复杂的页面可以使用上,可以把一个页面的功能拆分出来,使代码更简单.使用方式具体看 ...

  7. C++ 预处理器

    直接上代码 1.#define 预处理 #include <iostream> using namespace std; #define PI 3.14159 int main () { ...

  8. java StringBuilder和StringBuffer 用法

    可变的字符串,兄弟关系StringBuilder:效率高,安全性低StringBuffer:效率低,安全性高 StringBuilder 的常用方法的使用,StringBuffer用法一样. publ ...

  9. C语言程序设计II—第六周教学

    第六周教学总结(1/4-7/4) 教学内容 本周的教学内容为:第八章 指针 8.1 密码开锁(知识点:指针和指针变量的概念),8.2 角色互换(知识点:指针作为函数的参数返回多个值) 重点.难点:指针 ...

  10. 洛谷P1553 数字翻转(升级版)

    题目链接 https://www.luogu.org/problemnew/show/P1553 题目描述 给定一个数,请将该数各个位上数字反转得到一个新数. 这次与NOIp2011普及组第一题不同的 ...