Tuesday, August 18, 2015

Leetcode 89. Gray Code

https://leetcode.com/problems/gray-code/

Once we know the generating rule of gray code, this problem is simple.

Solution:
# T:O(2^n) S:O(1)
class Solution:
    # @return a list of integers
    def grayCode(self, n):
        res=[]
        size=1<<n
        for i in range(size):
            res.append((i>>1)^i)
        return res
Run Time: 56 ms

No comments:

Post a Comment