-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbit_manipulation.py
More file actions
39 lines (31 loc) · 847 Bytes
/
bit_manipulation.py
File metadata and controls
39 lines (31 loc) · 847 Bytes
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
class Solution:
# @param A : list of integers
# Modify the array A which is passed by reference.
# You do not need to return anything in this case.
def arrange(self, A):
B = [0]*len(A)
for i in range(len(A)):
B[i] = A[A[i]]
for i in range(len(A)):
A[i] = B[i+1]
# @param A : integer
# @return an integer
def numSetBits(self, A):
count = 0
while A:
A = A & A - 1
count += 1
return count
def countSetBits(self, n):
def numSetBits(A):
count = 0
while A:
A = A & A - 1
count += 1
return count
sum_ = 0
for i in range(n + 1):
sum_ += numSetBits(i)
return sum_
sols = Solution()
print(sols.numSetBits(11))