-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle-number-ii.py
55 lines (52 loc) · 2.49 KB
/
single-number-ii.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 137. Single Number II
# Ref: https://leetcode.com/problems/single-number-ii/
# Solution Ref: https://leetcode.com/problems/single-number-ii/solutions/3714928/bit-manipulation-c-java-python-beginner-friendly
# tags: bca, day-5, assignment, bit manipulation
class Solution:
def singleNumber(self, nums: List[int]) -> int:
# Initialize two variables, ones and twos,
# to keep track of the count of each bit position.
#
# ones: Tracks the bits that have appeared once.
# twos: Tracks the bits that have appeared twice.
ones = 0
twos = 0
# Iterate through the array of numbers.
# For each number `n` in the array:
# Update ones and twos:
# Let's analyze each step of the update process:
# a. ones = (ones ^ n) & (~twos);:
# ones ^ n XORs the current number `n` with the
# previous value of ones. This operation toggles
# the bits that have appeared an odd number of times,
# keeping the bits that have appeared twice unchanged.
# (~twos) negates the bits in twos, effectively removing
# the bits that have appeared twice from consideration.
# The & operation ensures that only the bits that
# have appeared once (after XOR) and not twice
# (after negating twos) are retained.
#
# b. twos = (twos ^ n) & (~ones);:
# twos ^ n XORs the current number n with
# the previous value of twos. This operation
# toggles the bits that have appeared
# an even number of times, effectively removing
# the bits that have appeared twice.
# (~ones) negates the bits in ones, effectively
# removing the bits that have appeared once from consideration.
# The & operation ensures that only the bits
# that have appeared twice (after XOR) and
# not once (after negating ones) are retained.
for n in nums:
ones ^= n & ~twos
twos ^= n & ~ones
# After iterating through all the numbers,
# the value stored in ones will represent
# the single number that appears only once in the array.
#
# In summary, the algorithm uses bit manipulation to efficiently
# keep track of the counts of each bit position.
# By utilizing XOR and AND operations, it can identify
# the bits of the single number that appears only once
# in the array while ignoring the bits that appear multiple times.
return ones