|
| 1 | +""" |
| 2 | +Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. |
| 3 | +
|
| 4 | +Example 1: |
| 5 | +
|
| 6 | +Input: |
| 7 | +[ |
| 8 | + [ 1, 2, 3 ], |
| 9 | + [ 4, 5, 6 ], |
| 10 | + [ 7, 8, 9 ] |
| 11 | +] |
| 12 | +Output: [1,2,3,6,9,8,7,4,5] |
| 13 | +Example 2: |
| 14 | +
|
| 15 | +Input: |
| 16 | +[ |
| 17 | + [1, 2, 3, 4], |
| 18 | + [5, 6, 7, 8], |
| 19 | + [9,10,11,12] |
| 20 | +] |
| 21 | +Output: [1,2,3,4,8,12,11,10,9,5,6,7] |
| 22 | +
|
| 23 | +
|
| 24 | +思路: |
| 25 | +
|
| 26 | +0, 0开始走, |
| 27 | +right -> down, |
| 28 | +down -> left, |
| 29 | +left -> up, |
| 30 | +up -> right. |
| 31 | +每走过一点,将值添加到结果中,走过的点记为 'x'。当四周都是 'x' 或边界时结束。 |
| 32 | +
|
| 33 | +测试地址: |
| 34 | +https://leetcode.com/problems/spiral-matrix/description/ |
| 35 | +
|
| 36 | +beat 99.10%. |
| 37 | +
|
| 38 | +""" |
| 39 | +def checkStop(matrix, x, y): |
| 40 | + t = [(x+1, y), |
| 41 | + (x-1, y), |
| 42 | + (x, y+1), |
| 43 | + (x, y-1), |
| 44 | + (x, y)] |
| 45 | + for i in t: |
| 46 | + try: |
| 47 | + if i[1] < 0 or i[0] < 0: |
| 48 | + continue |
| 49 | + |
| 50 | + if matrix[i[1]][i[0]] != 'x': |
| 51 | + return False |
| 52 | + except IndexError: |
| 53 | + continue |
| 54 | + else: |
| 55 | + return True |
| 56 | + |
| 57 | + |
| 58 | +class Solution(object): |
| 59 | + def right(self, matrix, x, y, result, stop): |
| 60 | + if checkStop(matrix, x, y): |
| 61 | + return result |
| 62 | + while 1: |
| 63 | + try: |
| 64 | + # matrix |
| 65 | + if matrix[y][x] == 'x': |
| 66 | + raise IndexError |
| 67 | + result.append(matrix[y][x]) |
| 68 | + matrix[y][x] = 'x' |
| 69 | + x += 1 |
| 70 | + |
| 71 | + except IndexError: |
| 72 | + x -= 1 |
| 73 | + return self.down(matrix, x, y+1, result, stop) |
| 74 | + |
| 75 | + def down(self, matrix, x ,y, result, stop): |
| 76 | + if checkStop(matrix, x, y): |
| 77 | + return result |
| 78 | + while 1: |
| 79 | + try: |
| 80 | + # matrix |
| 81 | + if matrix[y][x] == 'x': |
| 82 | + raise IndexError |
| 83 | + result.append(matrix[y][x]) |
| 84 | + matrix[y][x] = 'x' |
| 85 | + y += 1 |
| 86 | + except IndexError: |
| 87 | + y -= 1 |
| 88 | + return self.left(matrix, x-1, y, result, stop) |
| 89 | + |
| 90 | + def left(self, matrix, x, y, result, stop): |
| 91 | + if checkStop(matrix, x, y): |
| 92 | + return result |
| 93 | + while 1: |
| 94 | + try: |
| 95 | + # matrix |
| 96 | + if matrix[y][x] == 'x' or x < 0: |
| 97 | + raise IndexError |
| 98 | + result.append(matrix[y][x]) |
| 99 | + matrix[y][x] = 'x' |
| 100 | + x -= 1 |
| 101 | + except IndexError: |
| 102 | + x += 1 |
| 103 | + return self.up(matrix, x, y-1, result, stop) |
| 104 | + |
| 105 | + def up(self, matrix, x, y, result, stop): |
| 106 | + if checkStop(matrix, x, y): |
| 107 | + return result |
| 108 | + |
| 109 | + while 1: |
| 110 | + try: |
| 111 | + # matrix |
| 112 | + if matrix[y][x] == 'x' or y < 0: |
| 113 | + raise IndexError |
| 114 | + result.append(matrix[y][x]) |
| 115 | + matrix[y][x] = 'x' |
| 116 | + y -= 1 |
| 117 | + except IndexError: |
| 118 | + y += 1 |
| 119 | + return self.right(matrix, x+1, y, result, stop) |
| 120 | + |
| 121 | + def spiralOrder(self, matrix): |
| 122 | + """ |
| 123 | + :type matrix: List[List[int]] |
| 124 | + :rtype: List[int] |
| 125 | + """ |
| 126 | + x, y = 0, 0 |
| 127 | + |
| 128 | + result = [] |
| 129 | + # right -> down |
| 130 | + # down -> left |
| 131 | + # left -> up |
| 132 | + # up -> right |
| 133 | + stop = {} |
| 134 | + return self.right(matrix, 0, 0, result, stop) |
0 commit comments