Bit Others¶
Table of Contents¶
- 136. Single Number (Easy)
- 287. Find the Duplicate Number (Medium)
- 260. Single Number III (Medium)
- 2965. Find Missing and Repeated Values (Easy)
- 137. Single Number II (Medium)
- 645. Set Mismatch (Easy)
- 190. Reverse Bits (Easy)
- 371. Sum of Two Integers (Medium)
- 201. Bitwise AND of Numbers Range (Medium)
- 2154. Keep Multiplying Found Values by Two (Easy)
- 2044. Count Number of Maximum Bitwise-OR Subsets (Medium)
- 2438. Range Product Queries of Powers (Medium)
- 1680. Concatenation of Consecutive Binary Numbers (Medium)
- 1261. Find Elements in a Contaminated Binary Tree (Medium)
- 89. Gray Code (Medium)
- 1238. Circular Permutation in Binary Representation (Medium)
- 982. Triples with Bitwise AND Equal To Zero (Hard)
- 3307. Find the K-th Character in String Game II (Hard)
- 1611. Minimum One Bit Operations to Make Integers Zero (Hard)
- 751. IP to CIDR (Medium) 👑
- 3141. Maximum Hamming Distances (Hard) 👑
136. Single Number¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: array, bit manipulation
from functools import reduce
from operator import xor
from typing import List
# XOR
def singleNumber(nums: List[int]) -> int:
res = 0
for num in nums:
res ^= num
return res
# XOR
def singleNumberXOR(nums: List[int]) -> int:
return reduce(xor, nums)
# XOR
def singleNumberXORLambda(nums: List[int]) -> int:
return reduce(lambda x, y: x ^ y, nums)
nums = [4, 1, 2, 1, 2]
print(singleNumber(nums)) # 4
print(singleNumberXOR(nums)) # 4
print(singleNumberXORLambda(nums)) # 4
287. Find the Duplicate Number¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, two pointers, binary search, bit manipulation
- Find the duplicate number in an array containing
n + 1
integers where each integer is between1
andn
inclusive. - Floyd's Tortoise and Hare (Cycle Detection)
-
- Linked List Cycle
-
- Linked List Cycle II
-
- Time Complexity: O(n)
- Space Complexity: O(1)
Example: nums = [1, 3, 4, 2, 2]
0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|
1 | 3 | 4 | 2 | 2 |
graph LR
0((0)) --> 1((1))
1 --> 3((3))
2((2))--> 4((4))
3 --> 2
4 --> 2
from typing import List
# Floyd Cycle Detection Algorithm
def findDuplicate(nums: List[int]) -> int:
fast, slow = nums[0], nums[0]
while True:
slow = nums[slow]
fast = nums[nums[fast]]
if slow == fast:
break
slow = nums[0]
while slow != fast:
slow = nums[slow]
fast = nums[fast]
return slow
nums = [1, 3, 4, 2, 2]
print(findDuplicate(nums)) # 2
260. Single Number III¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, bit manipulation
2965. Find Missing and Repeated Values¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: array, hash table, math, matrix
137. Single Number II¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, bit manipulation
645. Set Mismatch¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: array, hash table, bit manipulation, sorting
190. Reverse Bits¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: divide and conquer, bit manipulation
# Bit Manipulation
def reverseBits(n: int) -> int:
res = 0
for i in range(32):
res = (res << 1) | (n & 1)
n >>= 1
return res
n = 0b00000010100101000001111010011100
print(reverseBits(n)) # 964176192
371. Sum of Two Integers¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: math, bit manipulation
# Bit Manipulation
def getSum(a: int, b: int) -> int:
MASK = 0xFFFFFFFF
MAX_INT = 0x7FFFFFFF
while b != 0:
temp = (a ^ b) & MASK
b = ((a & b) << 1) & MASK
a = temp
return a if a <= MAX_INT else ~(a ^ MASK)
print(getSum(1, 2)) # 3
201. Bitwise AND of Numbers Range¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: bit manipulation
2154. Keep Multiplying Found Values by Two¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: array, hash table, sorting, simulation
2044. Count Number of Maximum Bitwise-OR Subsets¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, backtracking, bit manipulation, enumeration
2438. Range Product Queries of Powers¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, bit manipulation, prefix sum
1680. Concatenation of Consecutive Binary Numbers¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: math, bit manipulation, simulation
1261. Find Elements in a Contaminated Binary Tree¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: hash table, tree, depth first search, breadth first search, design, binary tree
89. Gray Code¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: math, backtracking, bit manipulation
1238. Circular Permutation in Binary Representation¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: math, backtracking, bit manipulation
982. Triples with Bitwise AND Equal To Zero¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, hash table, bit manipulation
3307. Find the K-th Character in String Game II¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: math, bit manipulation, recursion
1611. Minimum One Bit Operations to Make Integers Zero¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: dynamic programming, bit manipulation, memoization
751. IP to CIDR¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: string, bit manipulation
3141. Maximum Hamming Distances¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, bit manipulation, breadth first search