LeetCode 089 Gray Code

LeetCode 089 Gray Code Problem

Download Code
class Solution(object):
    def grayCode(self, n):
        """
        :type n: int
        :rtype: List[int]
        """
        # https://leetcode.com/discuss/86617/6-line-java-solution-very-concise
        res = [0]
        for i in range(n):
            for j in reversed(range(len(res))):
                res.append(res[j] + (1 << i))
        return res


    # def count_one(self, num):
    #     count = 0
    #     while num:
    #         num &= (num - 1)
    #         count += 1
    #     return count

if __name__ == "__main__":
    s = Solution()
    print s.grayCode(2)

Download Gray Code.py

List of all Gray Code problems

Leetcode 089 Gray Code problem solution in python3 with explanation. This is the best place to expand your knowledge and get prepared for your next interview.

Feedback is the most important part of any website.

If you have any query, suggestion or feedback, Please feel free to contact us.