Python Recursive Solution.3 lines.


  • 0
    H
    #zip(*matrix [::-1]) :Rotate the matrix by 90 degrees (clockwise).   
    def generateMatrix(self, n):
        def gen(l, w, b):  #Generate a l*w Matrix. the begin number is b. 
            return l * [[]] and [range(b, b+l)] + zip(*gen(w-1, l, b+l)[::-1])
        return gen(n, n, 1)

  • 0
    J
    def generateMatrix(self, n):
        def gen(l, w, b):  #Generate a l*w Matrix. the begin number is b. 
            return l * [[]] and [range(b, b+l)] + map(list,zip(*gen(w-1, l, b+l)[::-1]))
        return gen(n, n, 1)
    

    the code should add map(list, *zip) to let the zip method return a list;


  • 0
    H

    Yeah, though it can be accepted. But strictly speaking, it should be return map(list, gen(n, n)).


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.