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. luogu P4146 序列终结者

    嘟嘟嘟 这是一道splay基础题. 最坑的一点是,因为有些节点可能没有左儿子或右儿子,所以必须把t[0].Max赋成-INF! 因为这个调了半天,看来回头复习复习splay是对的-- #include ...

  2. 30个你 “ 不可能全部会做 ” 的javascript题目

    1,以下表达式的运行结果是: ["1","2","3"].map(parseInt) A.["1","2&qu ...

  3. laravel框架入门

    本文摘自网络,个人感觉写的很不错,决定收藏一下纯属本人学习之用 本文介绍如何开始使用 Laravel. 读完本文,你将学到: 如何安装 Laravel,新建 Laravel 程序,如何连接数据库: L ...

  4. vue-cli 如何打包上线

    以vue创建的官方例子为例子,我们在开发环境的时候会 npm run dev ,生成 而想要打包成一份很简单, 只需要 npm run build 这个命令 这两种命令的配置文件在config的ind ...

  5. M100 组装教程

    http://bbs.dji.com/thread-37868-1-1.html

  6. 转载 AutoFac常见用法总结

    第二节:框架前期准备篇之AutoFac常见用法总结   一. 说在前面的话 凡是大约工作在两年以上的朋友们,或多或少都会接触到一些框架搭建方面的知识,只要一谈到框架搭建这个问题或者最佳用法这个问题,势 ...

  7. Redis的安装和客户端使用注意事项

    一.安装 (1)linux环境下: 获得软件包: wget http://download.redis.io/releases/redis-4.0.1.tar.gz 解压:tar -zxvf redi ...

  8. Matlab数据处理——数据的保存和读取方法操作

    1:dlmwrite()函数保存成txt文件 使用方法:      dlmwrite('filename', M)      使用默认分隔符“,”将矩阵M写入文本文件filename中:      d ...

  9. POJ2236

    https://vjudge.net/problem/POJ-2236 An earthquake takes place in Southeast Asia. The ACM (Asia Coope ...

  10. Luogu P1525 关押罪犯

    传送门 首先 这是一个并查集= = 这道题其实明白了还挺简单的qwq 思路: 因为只看仇恨值最大的一对儿,所以把他们从大到小排序,越大的就尽量分开,直到不能再分为止qwq q[x]表示x最大的敌人(x ...