We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。
m x n
m
n
Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1,2,3,6,9,8,7,4,5]
Input: [ [1, 2, 3, 4], [5, 6, 7, 8], [9,10,11,12] ] Output: [1,2,3,4,8,12,11,10,9,5,6,7]
The text was updated successfully, but these errors were encountered:
/** * @param {number[][]} matrix * @return {number[]} */ var spiralOrder = function(matrix) { if (matrix.length === 0 || matrix[0].length === 0) { return []; } const direction = [0, 1, 0, -1, 0]; const visited = new Set(); const reuslt = []; let patrolX = 0; let patrolY = -1; for (let i = 0; i < 4; i = (i + 1) % 4) { patrolX += direction[i]; patrolY += direction[i + 1] while( patrolX >= 0 && patrolX < matrix.length && patrolY >= 0 && patrolY < matrix[0].length && !visited.has(`${patrolX}-${patrolY}`) ) { visited.add(`${patrolX}-${patrolY}`); reuslt.push(matrix[patrolX][patrolY]); patrolX += direction[i]; patrolY += direction[i + 1]; } if (visited.size === matrix.length * matrix[0].length) { break; } else { patrolX -= direction[i]; patrolY -= direction[i + 1]; } } return reuslt; };
function spiralOrder(matrix: number[][]): number[] { if (matrix.length === 0 || matrix[0].length === 0) { return []; } const direction = [0, 1, 0, -1, 0]; const visited: Set<string> = new Set(); const reuslt: number[] = []; let patrolX = 0; let patrolY = -1; for (let i = 0; i < 4; i = (i + 1) % 4) { patrolX += direction[i]; patrolY += direction[i + 1] while( patrolX >= 0 && patrolX < matrix.length && patrolY >= 0 && patrolY < matrix[0].length && !visited.has(`${patrolX}-${patrolY}`) ) { visited.add(`${patrolX}-${patrolY}`); reuslt.push(matrix[patrolX][patrolY]); patrolX += direction[i]; patrolY += direction[i + 1]; } if (visited.size === matrix.length * matrix[0].length) { break; } else { patrolX -= direction[i]; patrolY -= direction[i + 1]; } } return reuslt; };
Sorry, something went wrong.
No branches or pull requests
54. Spiral Matrix
给定一个包含
m x n
个元素的矩阵(m
行,n
列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。Example 1
Example 2
The text was updated successfully, but these errors were encountered: