LeetCode-Algorithms-[Easy]面试题15. 二进制中1的个数

面试题15. 二进制中1的个数

	public int hammingWeight(int n) {
		int count = 0;
		while (n != 0) {
			count += n & 1;
			n = n >>> 1;
		}
		return count;
	}

	public int hammingWeight_2(int n) {
		String s = Integer.toBinaryString(n);
		int count = 0;
		for (int i = 0; i < s.length(); ++i) {
			if (s.charAt(i) == '1') {
				count++;
			}
		}
		return count;
	}

 

你可能感兴趣的:(LeetCode)