在不到 n 的时间内找到一个位串中两个连续的 1?
我试图找出一种方法来查看位串是否在位串大小为 n 的时间内有 2 个连续的位串。
例如,假设我们有一个大小为 5 的位串(索引 0-4)。如果索引 1 和 3 都是 0,我可以返回 false。但如果它们都是,那么我可能需要看 5 次才能找到答案。
位串的长度不必为 5。为了简单起见,假设它可以在 3 到 8 之间。
I am trying to figure out a way to see if a bitstring has 2 consecutive ones in the bitstring size n in less then n time.
For example, lets say we had a bitstring size 5 (index 0-4). If index 1 and 3 were both 0's, I could return false. But if they were both ones then I may have to do 5 peeks to find my answer.
The bitstring doesn't have to be length 5. For simplicity's sake, lets say it can be between 3 and 8.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的解决方案可能是将原始字符串与已左移或右移 1 位的自身版本进行按位“AND”操作。如果生成的位字符串非零,则其中至少有一个
11
:Simplest solution might be to bitwise
AND
the original string with a version of itself which has been shifted left or right by 1 bit. If the resulting bit string in non-zero then you have at least one11
in there: