最快的方法来检查,如果一个号码的“数字”在它的零哪儿了吗? [英] Fastest way to check if a number has the 'digit' zero anywhere in it?

查看:115
本文介绍了最快的方法来检查,如果一个号码的“数字”在它的零哪儿了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

怎么检查,如果一个号码的位数的最快的方法是0在任何地方?

What is the fastest way to check if a number has the digit '0' anywhere in it?

我需要建立一个快速的方法,因为我必须在$ $ 20秒内完成这些检查了近$ 10 ^ 9 $的数字。

I need to develop a fast method since i have to perform these checks for close to $10^9$ numbers in under $20$ seconds.

将为搜索零把它转换成字符串下班后?

Would searching for a zero after converting it into a string work?

推荐答案

由多个比$ 2 $是要采取行动的相同数目不管什么数是一个功率其它划分。因此,而不是反复除以$ X $ $ 10 $和测试每对$ 0 $剩余部分,​​考虑再三分$ X $ $ 10 ^ 6 $(说)和测试每个其余在查询表中的$ [0,10 ^ 6) $。查找表应该说是,如果余数包含一个内部零,否,如果它不包含零,以及也许如果余只有初始零(在这种情况下,检查$ X $当前是否处于非零和返回是或否相应)。

Dividing by a number other than a power of $2$ is going to take the same number of operations regardless of what the number is. So instead of repeatedly dividing $x$ by $10$ and testing each remainder against $0$, consider repeatedly dividing $x$ by $10^6$ (say) and testing each remainder against a lookup table on $[0, 10^6)$. The lookup table should say "yes" if the remainder contains an internal zero, "no" if it contains no zeros, and "maybe" if the remainder has only initial zeroes (in which case check whether $x$ is currently nonzero and return "yes" or "no" accordingly).

这篇关于最快的方法来检查,如果一个号码的“数字”在它的零哪儿了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆