Scribbling

LeetCode: 89. Gray Code 본문

Computer Science/Coding Test

LeetCode: 89. Gray Code

focalpoint 2021. 10. 8. 15:21

n = 0   n = 1   n=2  n = 3

000      001    101    1100

                   100    1101

                             1001

                             1000

e.g. n=3 -> 100 + 1000, 101 + 1000, 001 + 1000,  000 + 1000

            -> 역순 + 2의 n-1승

class Solution:
    def grayCode(self, n: int) -> List[int]:
        ret = [0]
        for i in range(n):
            ret += [x + pow(2, i) for x in reversed(ret)]
        return ret