LeetCode Number of 1 Bits 191
191. Number of 1 Bits Write a function that takes an unsigned integer and returns the number of ’1’ bits it has (also known as the Hamming weight). For example, the 32-bit integer ’11’ has binary representation 00000000000000000000000000001011, so the function should return 3. 大体意思 写一个函数,输入一个无符号整数,返回其中值为 1 的比特位的个数(这个值也被称为数字汉明重量) 自己的思路 循环判断最后一位是否是 1 public int hammingWeight(int n) { int result = 0; while (n != 0) { if ((n & 1) == 1) { result++; } n = n >> 1; } return result; } 在 Submit Solution ...