blob: 3d9f565ff7507208ee1527c37470b0841f6124d5 (
plain)
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
|
# 89. Gray Code
"""
an n-bit gray code sequence is a sequence of '2^n' integers where every
integer is in the inclusive range '[0, 2^n - 1]', the first integer is 0, an
integer appears no more than once in the sequence, the binary representation
of every pair of adjacent integers differs by exactly one bit and the binary
representation of the first and last integers differs by exactly one bit.
given an integer 'n', return any valid n-bit gray code sequence
"""
class Solution(object):
def grayCode(self, n):
"""
:type n: int
:rtype: List[int]
"""
ans = [0]
for i in range(n):
ans += [j + pow(2, i) for j in reversed(ans)]
return ans
if __name__ == "__main__":
obj = Solution()
print(obj.grayCode(n=2))
print(obj.grayCode(n=1))
|