class(object): defspiralOrder(self, matrix): """ :type matrix: List[List[int]] :rtype: List[int] """ ifnot matrix: return [] up = 0 down = len(matrix) - 1 left = 0 right = len(matrix[0]) - 1 rs = [] while right >= left and down >= up: if right >= left and down >= up: for i in xrange(left, right + 1): rs.append(matrix[up][i]) up += 1 if down >= up and right >= left: for i in xrange(up, down + 1): rs.append(matrix[i][right]) right -= 1 if right >= left and down >= up: for i in xrange(right, left - 1, -1): rs.append(matrix[down][i]) down -= 1 if down >= up and right >= left: for i in xrange(down, up - 1, -1): rs.append(matrix[i][left]) left += 1 return rs
近期评论