forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
design-bitset.py
73 lines (63 loc) · 1.54 KB
/
design-bitset.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Time: ctor: O(n)
# fix: O(1)
# unfix: O(1)
# flip: O(1)
# all: O(1)
# one: O(1)
# count: O(1)
# toString: O(n)
# Space: O(n)
# design
class Bitset(object):
def __init__(self, size):
"""
:type size: int
"""
self.__lookup = [False]*size
self.__flip = False
self.__cnt = 0
def fix(self, idx):
"""
:type idx: int
:rtype: None
"""
if self.__lookup[idx] == self.__flip:
self.__lookup[idx] = not self.__lookup[idx]
self.__cnt += 1
def unfix(self, idx):
"""
:type idx: int
:rtype: None
"""
if self.__lookup[idx] != self.__flip:
self.__lookup[idx] = not self.__lookup[idx]
self.__cnt -= 1
def flip(self):
"""
:rtype: None
"""
self.__flip = not self.__flip
self.__cnt = len(self.__lookup)-self.__cnt
def all(self):
"""
:rtype: bool
"""
return self.__cnt == len(self.__lookup)
def one(self):
"""
:rtype: bool
"""
return self.__cnt >= 1
def count(self):
"""
:rtype: int
"""
return self.__cnt
def toString(self):
"""
:rtype: str
"""
result = ['']*len(self.__lookup)
for i, x in enumerate(self.__lookup):
result[i] = '1' if x != self.__flip else '0'
return "".join(result)