-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy path079-bit_flip.py
57 lines (54 loc) · 1.74 KB
/
079-bit_flip.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
56
57
class Bits(object):
MAX_BITS = 32
def flip_bit(self, num):
if num is None:
raise TypeError('num cannot be None')
if num == -1:
return self.MAX_BITS
if num == 0:
return 1
seen = self._build_seen_list(num)
max_result = 0
looking_for = 0
for index, count in enumerate(seen):
result = 0
# Only look for zeroes
if looking_for == 1:
looking_for = not looking_for
continue
# First iteration, take trailing zeroes
# or trailing ones into account
if index == 0:
if count != 0:
# Trailing zeroes
try:
result = seen[index + 1] + 1
except IndexError:
result = 1
# Last iteration
elif index == len(seen) - 1:
result = 1 + seen[index - 1]
else:
# One zero
if count == 1:
result = seen[index + 1] + seen[index - 1] + 1
# Multiple zeroes
else:
result = max(seen[index + 1], seen[index - 1]) + 1
if result > max_result:
max_result = result
looking_for = not looking_for
return max_result
def _build_seen_list(self, num):
seen = []
looking_for = 0
count = 0
for _ in range(self.MAX_BITS):
if num & 1 != looking_for:
seen.append(count)
looking_for = not looking_for
count = 0
count += 1
num >>= 1
seen.append(count)
return seen