码迷,mamicode.com
首页 > 其他好文 > 详细

leetCode191-Number of 1 Bits

时间:2015-04-01 20:01:26      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:leetcode   191   

链接:https://leetcode.com/problems/number-of-1-bits/

此题关键是如何判断一个数字的第i为是否为0  即: x& (1<<i)

class Solution {
public:
    int hammingWeight(uint32_t n) {
        int count = 0;
        for(int i = 0; i < 32; i++){
            if((n & (1<<i)) != 0)count++;
        }
        return count;
        
    }
};


leetCode191-Number of 1 Bits

标签:leetcode   191   

原文地址:http://blog.csdn.net/lu597203933/article/details/44811049

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!