【问题描述】
输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。
时间限制:1秒 空间限制:32768K
【AC 代码】
Reference: https://blog.nowcoder.net/n/09d025ecf6b5486cb3ba3360dea03ada?f=comment
1 public class Solution { 2 public int NumberOf1(int n) { 3 int cnt = 0; 4 while (n != 0) { 5 cnt++; 6 n = (n-1) & n; 7 } 8 return cnt; 9 } 10 }
原文:https://www.cnblogs.com/moongazer/p/11579673.html