二进制中1的个数
C++代码class Solution { public: int hammingWeight(uint32_t n){ int res = 0; while(n){ res++; n = n & (n - 1); } return res; } };Java代码
public class Solution { // you need to treat n as an unsigned value public int hammingWeight(int n){ int res = 0; while(n != 0){ n = n & (n - 1); res++; } return res; } }联系方式
如果有任何问题可以邮箱联系我:raymondlam1@yeah.net
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)