这种解法的思路是,二进制中1的个数只与1的位数有关,n & (n - 1)快速的去掉最左边的1,例如7(0111) & 6(0110)= 6(0110),快速的去掉了最左边的1
/** * Created by zj on 2017/3/11 0011. */ public class NumberOf1 { int count = 0; int numberOf1(int n){ while(n != 0){ count++; n = n & (n-1); } return count; } }