Binary
Binary
0.1 Sum-of-two-integers
Given two integers a and b, return the sum of the two integers without using the operators + and
-.
Example 1:
Input: a = 1, b = 2 Output: 3
Example 2:
Input: a = 2, b = 3 Output: 5
[ ]: class Solution:
def getSum(self, a: int, b: int) -> int:
mask = 0xFFFFFFFF
Max_int_mask = 0x7FFFFFFF
if b == 0:
# If a exceeds Max_int_mask, it means it's negative in two's␣
↪complement
0.2 Number-of-1-bits
Given a positive integer n, write a function that returns the number of set bits in its binary
representation (also known as the Hamming weight).
Example 1: Input: n = 11 Output: 3
Explanation: The input binary string 1011 has a total of three set bits.
Example 2: Input: n = 128 Output: 1
Explanation: The input binary string 10000000 has a total of one set bit.
[ ]: class Solution:
def hammingWeight(self, n: int) -> int:
def hammingWeight_helper(n):
1
if n == 0 or n == 1:
return n
return n%2 + hammingWeight_helper(n//2)
return hammingWeight_helper(n)
0.3 Counting-bits
Given an integer n, return an array ans of length n + 1 such that for each i (0 <= i <= n), ans[i]
is the number of 1’s in the binary representation of i.
Example 1:
Input: n = 2 Output: [0,1,1] Explanation: 0 –> 0 1 –> 1 2 –> 10
Example 2:
Input: n = 5 Output: [0,1,1,2,1,2] Explanation: 0 –> 0 1 –> 1 2 –> 10 3 –> 11 4 –> 100 5 –> 101
[ ]: class Solution:
def countBits(self, n: int) -> List[int]:
res = [0]*(n+1)
for i in range(n+1):
res[i] = res[i>>1] + (i&1)
return res
# for i in range(n+1):
# bit_i = countBits_helper(i)
# res.append(bit_i)
0.4 Missing-number
Given an array nums containing n distinct numbers in the range [0, n], return the only number in
the range that is missing from the array.
Example 1:
Input: nums = [3,0,1] Output: 2 Explanation: n = 3 since there are 3 numbers, so all numbers are
in the range [0,3]. 2 is the missing number in the range since it does not appear in nums.
Example 2:
Input: nums = [0,1] Output: 2 Explanation: n = 2 since there are 2 numbers, so all numbers are
in the range [0,2]. 2 is the missing number in the range since it does not appear in nums. Example
2
3:
Input: nums = [9,6,4,2,3,5,7,0,1] Output: 8 Explanation: n = 9 since there are 9 numbers, so all
numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in
nums.
[ ]: class Solution:
def missingNumber(self, nums: List[int]) -> int:
sum_li = sum(nums)
sum_n = 0
for i in range(len(nums)+1):
sum_n += i
return sum_n - sum_li
0.5 Reverse-bits
Reverse bits of a given 32 bits unsigned integer.
Note:
Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input
and output will be given as a signed integer type. They should not affect your implementation, as
the integer’s internal binary representation is the same, whether it is signed or unsigned. In Java,
the compiler represents the signed integers using 2’s complement notation. Therefore, in Example
2 above, the input represents the signed integer -3 and the output represents the signed integer
-1073741825.
Example 1:
Input: n = 00000010100101000001111010011100 Output: 964176192
(00111001011110000010100101000000) Explanation: The input binary string
00000010100101000001111010011100 represents the unsigned integer 43261596, so return
964176192 which its binary representation is 00111001011110000010100101000000. Example 2:
Input: n = 11111111111111111111111111111101 Output: 3221225471
(10111111111111111111111111111111) Explanation: The input binary string
11111111111111111111111111111101 represents the unsigned integer 4294967293, so return
3221225471 which its binary representation is 10111111111111111111111111111111.Reverse bits of
a given 32 bits unsigned integer.
Note:
Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input
and output will be given as a signed integer type. They should not affect your implementation, as
the integer’s internal binary representation is the same, whether it is signed or unsigned. In Java,
the compiler represents the signed integers using 2’s complement notation. Therefore, in Example
2 above, the input represents the signed integer -3 and the output represents the signed integer
-1073741825.
Example 1:
Input: n = 00000010100101000001111010011100 Output: 964176192
(00111001011110000010100101000000) Explanation: The input binary string
3
00000010100101000001111010011100 represents the unsigned integer 43261596, so return
964176192 which its binary representation is 00111001011110000010100101000000. Example 2:
Input: n = 11111111111111111111111111111101 Output: 3221225471
(10111111111111111111111111111111) Explanation: The input binary string
11111111111111111111111111111101 represents the unsigned integer 4294967293, so return
3221225471 which its binary representation is 10111111111111111111111111111111.
[ ]: class Solution:
def reverseBits(self, n: int) -> int:
res = 0
for i in range(32):
# get the ith bit from n
bit_i = (n >> i) & 1
res = res | (bit_i << (31 - i))
return res