伯爵在二重presentation 1的号码 [英] Count number of 1's in binary representation

查看:109
本文介绍了伯爵在二重presentation 1的号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有效的方式来计数的数量为O二进制重新presentation 1的个数(1)如果你有足够的内存一起玩。这是一个面试问题,我发现在一个网上论坛,但一直无人接听。有人可以提出建议,我不能想办法做到在O(1)的时间?

Efficient way to count number of 1s in the binary representation of a number in O(1) if you have enough memory to play with. This is an interview question I found on an online forum, but it had no answer. Can somebody suggest something, I cant think of a way to do it in O(1) time?

推荐答案

这就是汉明权重的问题,又名人口数。链接提及高效的实现。引用:

That's the Hamming weight problem, a.k.a. population count. The link mentions efficient implementations. Quoting:

通过无限的记忆,我们可以简单地创建的每个64位整数的汉明权的大型查找表

With unlimited memory, we could simply create a large lookup table of the Hamming weight of every 64 bit integer

这篇关于伯爵在二重presentation 1的号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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