标签:上下 strong ted index == ret tom offer class
题目描述
输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。
思路一:顺时针旋转路径
从左上角出发,初始方向向右,当超出边界或进入之前访问过的区域时,顺时针旋转方向。
要点:
代码:
var spiralOrder = function(matrix) { if(matrix == null || matrix.length === 0) return []; const rows = matrix.length, cols= matrix[0].length; const total = rows * cols; let visited = new Array(); for(let i = 0; i < rows; i++){ visited[i] = new Array(); for(let j = 0; j < cols; j++){ visited[i][j] = 0; } } let res = []; const directions = [[0, 1], [1, 0], [0, -1], [-1, 0]]; let directionIndex = 0; let i = 0, j = 0; for(let n = 0; n < total; n++){ res[n] = matrix[i][j]; visited[i][j] = 1; let nextRow = i + directions[directionIndex][0]; let nextCol = j + directions[directionIndex][1]; if(nextRow >= rows || nextRow < 0 || nextCol >= cols || nextCol < 0 || visited[nextRow][nextCol]){ directionIndex = (directionIndex + 1) % 4; } i = i + directions[directionIndex][0]; j = j + directions[directionIndex][1]; } return res; };
易错点:
思路二: 一层一层打印
用 left, right, top, buttom 标记当前打印的层数的边界,最后一行/一列单独打印。
代码:
var spiralOrder = function(matrix) { if(matrix == null || matrix.length === 0) return []; let rows = matrix.length, cols = matrix[0].length; let left = 0, right = cols - 1, top = 0, buttom = rows - 1; let res = []; while(left < right && top < buttom){ //上 for(let j = left; j < right; j++){ res.push(matrix[top][j]); } //右 for(let i = top; i < buttom; i++){ res.push(matrix[i][right]); } //下 for(let j = right; j > left; j--){ res.push(matrix[buttom][j]); } //左 for(let i = buttom; i > top; i--){ res.push(matrix[i][left]); } left++; right--; top++; buttom--; } //最后一列 if(left === right){ for(let i = top; i <= buttom; i++){ res.push(matrix[i][left]) } } //最后一行 else if(top === buttom){ for(let j = left; j <= right; j++){ res.push(matrix[top][j]); } } return res; };
标签:上下 strong ted index == ret tom offer class
原文地址:https://www.cnblogs.com/xintangchn/p/13171014.html