Integer -> 4 bytes -> 32 bits
Long -> 8 bytes -> 64 bits
Boolean/Char -> 1 byte
Given an array of integers, every element appears twice except for one. Find that single one.
public int singleNumber(int[] A) {
for (int i = 1; i < A.length; i++) {
A[i] ^= A[i-1];
}
return A[A.length-1];
}
Given an array of integers, every element appears twice except for one. Find that single one.
def singleNumber(self, A: List[int]) -> int:
for i in range(1, len(A)):
A[i] ^= A[i-1]
return A[-1]
Write a function that takes an unsigned integer and returns the number of ’1' bits it has. (also known as the Hamming weight)
public int hammingWeight(int n) {
int result = 0;
for (int i = 0; i < 32; i++) {
result += n & 1;
n >>= 1;
}
return result;
}
Write a function that takes an unsigned integer and returns the number of ’1' bits it has. (also known as the Hamming weight)
def hammingWeight(self, n: int) -> int:
result = 0
for i in range(32):
result += n & 1
n >>= 1
return result