题目描述:

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].

要完成的函数:

int rotatedDigits(int N)

说明:

1、这道题给定一个数字N,要求判断在[1,N]之间的数有多少个“好数”。好数的判断标准是这样的:数位上的每个数字翻转180度,翻转之后的所有数位还能构成数字的,并且该数字与翻转之前的不一样,那么就是一个好数。

数字0/1/8,180度翻转之后还是本身,数字3/4/7,180度翻转之后形成不了数字,数字2和数字5翻转之后是对方,数字6和数字9翻转之后也是对方。

比如数字17,反转之后不是数字,那么不是好数。比如数字18,翻转之后还是数字18,也不是好数。比如数字19,翻转之后是16,是好数。

2、明白题意之后,这道题笔者最开始想要看看有没有什么数学规律,能够比较便捷地计算,但后来发现,好像还是暴力迭代最容易做了。

不过暴力迭代法中间也有技巧。

碰到一个数字,不断地对10取余数,取出末位,判断末位是三种数字中的哪一种。

这个判断如果一个个去判断,还是很慢。可以用set.count(),会快很多。但还有一种更快的做法,如下述代码:

    int rotatedDigits(int N)
{
vector<int>judge={1,1,2,0,0,2,2,0,1,2};//0表示形成不了有效数字
int j,t,count=0; //2表示出现了2/5/6/9
bool flag=0;
for(int i=1;i<=N;i++)
{
j=i;
flag=0;
while(j)
{
t=j%10;
if(judge[t]==0)
{
flag=0;//这里要flag=0,不然比如32,就出错了
break;
}
else if(judge[t]==2)
flag=1;
j/=10;
}
if(flag==1)
count++;
}
return count; }

上述代码实测9ms,beats 37.13% of cpp submissions。

3、改进:

看了一些discuss的代码,发现大家大体上都是暴力迭代法实现的,但是他们就是能跑到4ms……

看了他们的方法,自己实现了一下,代码分享给大家,如下:

    bool isvalid(int n)
{
bool flag=false;
while(n>0)
{
if(n%10==2)
flag=true;
else if(n%10==5)
flag = true;
else if(n%10==6)
flag=true;
else if(n%10==9)
flag=true;
else if(n%10==3)
return false;
else if(n%10==4)
return false;
else if(n%10==7)
return false;
n/=10;
}
return flag;
}
int rotatedDigits(int N)
{
int res=0;
for(int i=1;i<=N;i++)
{
if(isvalid(i))
res++;
}
return res;
}

代码思想大同小异,笔者2中的代码,也是逐个判断是否为好数,然后count++。但是判断的过程不一样。

笔者在函数内部实现判断,上述代码是另外定义一个函数来判断,照理来说,调用函数这个过程会花费一些时间。

此外,笔者的代码多了一个要判断flag的值是否为1的过程,而上述代码没有这个过程。

总的来说,比起大神的代码,多了判断flag==1的的过程,少了函数调用的过程,这样能省5ms……

上述代码实测4ms,beats 95.00% of cpp submissions。

leetcode-788-Rotated Digits(使用vector替代if else的逐个判断)的更多相关文章

  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

    https://leetcode.com/problems/rotated-digits/ X is a good number if after rotating each digit indivi ...

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

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

  4. 【Leetcode_easy】788. Rotated Digits

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

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

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

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

  7. 788. Rotated Digits

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

  8. 788. Rotated Digits 旋转数字

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

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

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

随机推荐

  1. 微信小程序(应用号)开发教程

    本文档将带你一步步创建完成一个微信小程序,并可以在手机上体验该小程序的实际效果.这个小程序的首页将会显示欢迎语以及当前用户的微信头像,点击头像,可以在新开的页面中查看当前小程序的启动日志.下载源码 1 ...

  2. oracle数据库学习记录(持续更新中...)

    --------------------------------------------day1------------------------------------------------- 1. ...

  3. nyoj746 整数划分

    nyoj746 http://acm.nyist.net/JudgeOnline/problem.php?pid=746 一道区间dp的题目: 设:a[i][j]为那一串数字中从第i位到第j位的数是多 ...

  4. [Groovy]获取当前活动的Environment,获取response中节点的name和节点的value

    import com.eviware.soapui.support.GroovyUtils import com.eviware.soapui.support.XmlHolder import org ...

  5. C#变量初始化

    在C#中声明变量使用下述语法: datatype identifier;, 例如: int i; 该语句声明int变量i.编译器不允许在表达式中使用这个变量,除非用一个值初始化了改变量.如果你不需要使 ...

  6. 05 Computing GC Content

    Problem The GC-content of a DNA string is given by the percentage of symbols in the string that are ...

  7. [GO]ticker的使用

    package main import ( "time" "fmt" ) //ticker是一个定时触发的计时器,它会以一个间隔往channel发送整一个事件( ...

  8. ping别的电脑出错

    原因ifconfig 电脑1:172.31.45.101 电脑2:172.31.188.232 http://ask.csdn.net/questions/178358 如何防止别人ping自己的电脑 ...

  9. 风尘浪子 只要肯努力,梦想总有一天会实现 WF工作流与Web服务的相互调用 —— 通过Web服务调用Workflow工作流(开发持久化工作流) _转

    如果你曾经负责开发企业ERP系统或者OA系统,工作流对你来说一定并不陌生.工作流(Workflow)是对工作流程及其各操作步骤之间业务规则 的抽象.概括.描述.工作流要解决的主要问题是:为实现某个业务 ...

  10. SOLR企业搜索平台 二 (分词安装)

    标签:linux lucene 分词 solr 全文检索 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://3961409.blog ...