One-line solution in Python,obviously not in-place


  • 2
    M
    class Solution:
    # @param matrix, a list of lists of integers
    # @return a list of lists of integers
    def rotate(self, matrix):
        return map(lambda x:map(lambda y:y.pop(0),matrix[::-1]),range(len(matrix)))

  • 1
    C

    How about this one liner:

     def rotate(self, matrix):
         return [list(col[::-1]) for col in zip(*matrix)]

  • 1
    H

    I believe I can end "one-line python" topic like this:

    zip(*original[::-1])
    # or counter-clockwise
    zip(*original)[::-1]

  • 0
    V

    a bit of cheating, but it works.

    matrix[:] = zip(*matrix[::-1])

Log in to reply
 

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