Flatten Binary Tree to Linked List (Python)
Restore IP Addresses (Python)

Rotate Image (Python)

kitt posted @ 2014年2月09日 10:59 in LeetCode , 1622 阅读

In-place的话,先以matrix[0][n-1]...matrix[n-1][0]对角线为轴交换元素,再以水平中线(即第(n+1)/2行)交换元素。

class Solution:
    # @param matrix, a list of lists of integers
    # @return a list of lists of integers
    def rotate(self, matrix):
        n = len(matrix)
        for i in xrange(n):
            for j in xrange(n-1-i):
                matrix[i][j], matrix[n-1-j][n-1-i] = matrix[n-1-j][n-1-i], matrix[i][j]
        for i in xrange(n/2):
            for j in xrange(n):
                matrix[i][j], matrix[n-1-i][j] = matrix[n-1-i][j], matrix[i][j]
        return matrix

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter