forked from shichao-an/leetcode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.py
More file actions
14 lines (14 loc) · 403 Bytes
/
solution.py
File metadata and controls
14 lines (14 loc) · 403 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution:
# @return a list of integers
def grayCode(self, n):
m = 1 << n
res = []
d = [(1 << (i + 1)) / 2 for i in range(n)]
for i in range(m):
num = 0
for j, e in enumerate(d):
if e / (1 << (j + 1)) % 2 == 1:
num += 1 << j
d[j] += 1
res.append(num)
return res