在不到 n 的时间内找到一个位串中两个连续的 1?

发布于 2024-09-30 06:40:01 字数 182 浏览 0 评论 0原文

我试图找出一种方法来查看位串是否在位串大小为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿港小镇 2024-10-07 06:40:01

最简单的解决方案可能是将原始字符串与已左移或右移 1 位的自身版本进行按位“AND”操作。如果生成的位字符串非零,则其中至少有一个 11

test = (src & (src << 1));

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 one 11 in there:

test = (src & (src << 1));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文