输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
1 # -*- coding:utf-8 -*- 2 class Solution: 3 # matrix类型为二维列表,需要返回列表 4 def __init__(self): 5 self.result = [] 6 self.visited = [] 7 8 def goRight(self,matrix,i,j,row,column,direct): 9 if j + 1 < column and self.visited[i][j+1] == 0: 10 self.result.append(matrix[i][j+1]) 11 self.visited[i][j+1] = 1 12 self.dfs(matrix,i,j+1,row,column,direct) 13 14 def goDown(self,matrix,i,j,row,column,direct): 15 if i + 1 < row and self.visited[i+1][j] == 0: 16 self.result.append(matrix[i+1][j]) 17 self.visited[i+1][j] = 1 18 self.dfs(matrix,i+1,j,row,column,direct) 19 20 def goLeft(self,matrix,i,j,row,column,direct): 21 if j - 1 >= 0 and self.visited[i][j-1] == 0: 22 self.result.append(matrix[i][j-1]) 23 self.visited[i][j-1] = 1 24 self.dfs(matrix,i,j-1,row,column,direct) 25 26 def goUp(self,matrix,i,j,row,column,direct): 27 if i - 1 >= 0 and self.visited[i-1][j] == 0: 28 self.result.append(matrix[i-1][j]) 29 self.visited[i-1][j] = 1 30 self.dfs(matrix,i-1,j,row,column,direct) 31 32 def dfs(self,matrix,i,j,row,column,direct): 33 if direct == 0: 34 self.goRight(matrix,i,j,row,column,0) 35 self.goDown(matrix,i,j,row,column,1) 36 self.goLeft(matrix,i,j,row,column,2) 37 self.goUp(matrix,i,j,row,column,3) 38 if direct == 1: 39 self.goDown(matrix,i,j,row,column,1) 40 self.goLeft(matrix,i,j,row,column,2) 41 self.goUp(matrix,i,j,row,column,3) 42 self.goRight(matrix,i,j,row,column,0) 43 if direct == 2: 44 self.goLeft(matrix,i,j,row,column,2) 45 self.goUp(matrix,i,j,row,column,3) 46 self.goRight(matrix,i,j,row,column,0) 47 self.goDown(matrix,i,j,row,column,1) 48 if direct == 3: 49 self.goUp(matrix,i,j,row,column,3) 50 self.goRight(matrix,i,j,row,column,0) 51 self.goDown(matrix,i,j,row,column,1) 52 self.goLeft(matrix,i,j,row,column,2) 53 54 def printMatrix(self, matrix): 55 row = len(matrix) 56 column = len(matrix[0]) 57 self.visited = [[0 for c in range(column)] for r in range(row)] 58 self.result.append(matrix[0][0]) 59 self.visited[0][0] = 1 60 self.dfs(matrix,0,0,row,column,0) 61 return self.result 62 # write code here
Java版代码,leetcode地址:
1 public int[] spiralOrder(int[][] matrix) { 2 if (matrix.length == 0) { 3 return new int[0]; 4 } 5 int row = matrix.length; 6 if (matrix[0].length == 0) { 7 return matrix[0]; 8 } 9 int column = matrix[0].length; 10 ArrayList<Integer> list = new ArrayList<Integer>(); 11 int row_begin = 0; 12 int row_end = row - 1; 13 int col_begin = 0; 14 int col_end = column - 1; 15 while (row_begin <= row_end && col_begin <= col_end) { 16 // 向右 17 int right_i = row_begin; 18 for (int right_j = col_begin; right_j <= col_end; right_j++) { 19 list.add(matrix[right_i][right_j]); 20 } 21 row_begin++; 22 if (row_begin > row_end) { 23 break; 24 } 25 26 // 向下 27 int down_j = col_end; 28 for (int down_i = row_begin; down_i <= row_end; down_i++) { 29 list.add(matrix[down_i][down_j]); 30 } 31 col_end--; 32 if (col_begin > col_end) { 33 break; 34 } 35 36 // 向左 37 int left_i = row_end; 38 for (int left_j = col_end; left_j >= col_begin; left_j--) { 39 list.add(matrix[left_i][left_j]); 40 } 41 row_end--; 42 if (row_begin > row_end) { 43 break; 44 } 45 46 // 向上 47 int up_j = col_begin; 48 for (int up_i = row_end; up_i >= row_begin; up_i--) { 49 list.add(matrix[up_i][up_j]); 50 } 51 col_begin++; 52 if (col_begin > col_end) { 53 break; 54 } 55 } 56 return list.stream().mapToInt(Integer::valueOf).toArray(); 57 }
思路:缩圈。
先定义循环的边界,即:左上,右上,右下,左下。
向右遍历后,上侧的边界向下缩小一行,即上边界+1。
向下遍历后,右侧的边界向左缩小一列,即右边界-1。
向左遍历后,下侧的边界向上缩小一行,即下边界-1。
向上遍历后,左侧的边界向右缩小一列,即左边界+1。
注意判断,已经是否已经遍历过了单元格,如果已经遍历过就结束循环。