【剑指Offer学习】【面试题10 :二进制中1 的个数】

https://blog.csdn.net/derrantcm/article/details/45476103

int totalOne(int n) {
    int nInput = n >= 0 ? n : -n;
    int total = 0;
    while (nInput != 0) {
        if(nInput % 2 == 1) {
            total++;
        }
        nInput /=2;
    }
    return total;
}

int totalOneNew(int n) {
    int nInput = n >= 0 ? n : -n;
    int total = 0;
    while (nInput != 0) {
        nInput = nInput & (nInput-1);
        total++;
    }
    return total;
}
发布了81 篇原创文章 · 获赞 68 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/li198847/article/details/104412104
今日推荐