博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
788. Rotated Digits
阅读量:4704 次
发布时间:2019-06-10

本文共 1839 字,大约阅读时间需要 6 分钟。

这里给出两种方法

方法一,逐个数字逐位判定,速度有点慢,但是很好理解

1 class Solution  2 { 3 public: 4     int rotatedDigits(int N)  5     { 6         unordered_set
unvalid{
3,4,7}; 7 unordered_set
change{
2,5,6,9}; 8 int count=0; 9 for(int i=2;i<=N;i++)10 {11 int cur=i;12 int flag=0;13 while(cur)14 {15 int last=cur%10;16 if(unvalid.find(last)!=unvalid.end())17 {18 flag=0;19 break;20 }21 if(change.find(last)!=change.end())22 flag=1;23 cur/=10;24 }25 count+=flag;26 }27 return count;28 }29 };

方法二,利用前面的判定结果,加快判定速度

1 class Solution  2 { 3 public: 4     int rotatedDigits(int N)  5     { 6         vector
judge(N+1,0); 7 int count=0; 8 for(int i=0;i<=N;i++) 9 {10 if(i<10)11 {12 if(i==0||i==1||i==8)13 judge[i]=1;14 else if(i==3||i==4||i==7)15 judge[i]=0;16 else17 {18 judge[i]=2;19 count++;20 }21 }22 else23 {24 int pre=judge[i/10];25 int last=judge[i%10];26 if(pre==1&&last==1)27 judge[i]=1;28 else if(pre+last>2)29 {30 judge[i]=2;31 count++;32 }33 else34 judge[i]=0;35 }36 }37 return count;38 }39 };

 

转载于:https://www.cnblogs.com/zhuangbijingdeboke/p/9179214.html

你可能感兴趣的文章
斑马为什么有条纹?
查看>>
android多层树形结构列表学习笔记
查看>>
Android_去掉EditText控件周围橙色高亮区域
查看>>
《构建之法》第一、二、十六章阅读笔记
查看>>
arrow:让Python的日期与时间变的更好
查看>>
(转)Excel的 OleDb 连接串的格式(连接Excel 2003-2013)
查看>>
Java并发编程
查看>>
Git Stash用法
查看>>
sql server 2008学习8 sql server存储和索引结构
查看>>
Jquery radio选中
查看>>
memcached 细究(三)
查看>>
RSA System.Security.Cryptography.CryptographicException
查看>>
webservice整合spring cxf
查看>>
[解题报告] 100 - The 3n + 1 problem
查看>>
Entity Framework 学习高级篇1—改善EF代码的方法(上)
查看>>
Mybatis逆向工程配置文件详细介绍(转)
查看>>
String类的深入学习与理解
查看>>
不把DB放进容器的理由
查看>>
OnePage收集
查看>>
Java parseInt()方法
查看>>