Skip to content

Latest commit

 

History

History
29 lines (24 loc) · 694 Bytes

89.md

File metadata and controls

29 lines (24 loc) · 694 Bytes
class Solution(object):
    def grayCode(self, n):
        """
        :type n: int
        :rtype: List[int]
        """
        if n == 0:
            return [0]
        
        stack = ['0', '1']
        stackout = ['0', '1']
        n -= 1
        for i in range(n):
            stack.reverse()
            for i in range(len(stackout)):
                stackout[i] = stackout[i].zfill(len(stackout[-1])+1)
            for op in stack:
                stackout.append('1'+op)
            stack = [x for x in stackout]
            
        for i in range(len(stackout)):
            stackout[i] = int(stackout[i],2)
        
        return stackout
        

字符串补0 栈