王廷瑋|數位醫療|智慧醫療: 54. Spiral Matrix WFU

2024年7月4日 星期四

54. Spiral Matrix

54. Spiral Matrix


給定一個 m x n 的矩陣,按螺旋順序返回矩陣中的所有元素。

範例 1:

輸入:matrix = [[1,2,3],[4,5,6],[7,8,9]] 輸出:[1,2,3,6,9,8,7,4,5]


Python

from typing import List

class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
if not matrix or not matrix[0]:
return []
result = []
top, bottom = 0, len(matrix) - 1
left, right = 0, len(matrix[0]) - 1
while top <= bottom and left <= right:
# Traverse from left to right along the top boundary
for col in range(left, right + 1):
result.append(matrix[top][col])
top += 1
# Traverse from top to bottom along the right boundary
for row in range(top, bottom + 1):
result.append(matrix[row][right])
right -= 1
if top <= bottom:
# Traverse from right to left along the bottom boundary
for col in range(right, left - 1, -1):
result.append(matrix[bottom][col])
bottom -= 1
if left <= right:
# Traverse from bottom to top along the left boundary
for row in range(bottom, top - 1, -1):
result.append(matrix[row][left])
left += 1
return result

16.58MB, 26ms


C++


#include <vector>

using namespace std;

class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> result;
if (matrix.empty() || matrix[0].empty()) return result;
int top = 0;
int bottom = matrix.size() - 1;
int left = 0;
int right = matrix[0].size() - 1;
while (top <= bottom && left <= right) {
// Traverse from left to right along the top boundary
for (int col = left; col <= right; ++col) {
result.push_back(matrix[top][col]);
}
++top;
// Traverse from top to bottom along the right boundary
for (int row = top; row <= bottom; ++row) {
result.push_back(matrix[row][right]);
}
--right;
if (top <= bottom) {
// Traverse from right to left along the bottom boundary
for (int col = right; col >= left; --col) {
result.push_back(matrix[bottom][col]);
}
--bottom;
}
if (left <= right) {
// Traverse from bottom to top along the left boundary
for (int row = bottom; row >= top; --row) {
result.push_back(matrix[row][left]);
}
++left;
}
}
return result;
}
};

8.13MB, 3ms


Javascript


/**
* @param {number[][]} matrix
* @return {number[]}
*/
var spiralOrder = function(matrix) {
if (!matrix || matrix.length === 0 || matrix[0].length === 0) {
return [];
}

const result = [];
let top = 0;
let bottom = matrix.length - 1;
let left = 0;
let right = matrix[0].length - 1;

while (top <= bottom && left <= right) {
// Traverse from left to right along the top boundary
for (let col = left; col <= right; col++) {
result.push(matrix[top][col]);
}
top++;

// Traverse from top to bottom along the right boundary
for (let row = top; row <= bottom; row++) {
result.push(matrix[row][right]);
}
right--;

if (top <= bottom) {
// Traverse from right to left along the bottom boundary
for (let col = right; col >= left; col--) {
result.push(matrix[bottom][col]);
}
bottom--;
}

if (left <= right) {
// Traverse from bottom to top along the left boundary
for (let row = bottom; row >= top; row--) {
result.push(matrix[row][left]);
}
left++;
}
}

return result;
};

48.52MB, 56ms