zl程序教程

您现在的位置是:首页 >  其他

当前栏目

[LeetCode] 54. Spiral Matrix 螺旋矩阵

LeetCode 矩阵 matrix 54 螺旋
2023-09-27 14:28:37 时间

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.

For example,
Given the following matrix:

[
 [ 1, 2, 3 ],
 [ 4, 5, 6 ],
 [ 7, 8, 9 ]
]

You should return [1,2,3,6,9,8,7,4,5].

将一个矩阵按螺旋顺序输出。

解法:用变量left, right, top, bottom记录左,右,顶,底。然后按照左到右,顶到底,右到左,底到顶的顺序循环,把遍历的元素加入到结果。

Java:

public class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> res = new ArrayList<>();
        if(matrix == null || matrix.length == 0)
            return res;
        int rowNum = matrix.length, colNum = matrix[0].length; 
        int left = 0, right = colNum - 1, top = 0, bot = rowNum - 1;
        
        while(res.size() < rowNum * colNum) {
            for(int col = left; col <= right; col++)
                res.add(matrix[top][col]);
            top++;
            if(res.size() < rowNum * colNum) {
                for(int row = top; row <= bot; row++)
                    res.add(matrix[row][right]);
                right--;    
            }
            if(res.size() < rowNum * colNum) {
                for(int col = right; col >= left; col--)
                    res.add(matrix[bot][col]);
                bot--;
            }
            if(res.size() < rowNum * colNum) {
                for(int row = bot; row >= top; row--)
                    res.add(matrix[row][left]);
                left++;
            }
        }
        
        return res;
    }
}  

Python:

class Solution:
    # @param matrix, a list of lists of integers
    # @return a list of integers
    def spiralOrder(self, matrix):
        result = []
        if matrix == []:
            return result
        
        left, right, top, bottom = 0, len(matrix[0]) - 1, 0, len(matrix) - 1
        
        while left <= right and top <= bottom:
            for j in xrange(left, right + 1):
                result.append(matrix[top][j])
            for i in xrange(top + 1, bottom):
                result.append(matrix[i][right])
            for j in reversed(xrange(left, right + 1)):
                if top < bottom:
                    result.append(matrix[bottom][j])
            for i in reversed(xrange(top + 1, bottom)):
                if left < right:
                    result.append(matrix[i][left])
            left, right, top, bottom = left + 1, right - 1, top + 1, bottom - 1
            
        return result  

C++:

class Solution {
public:
    vector<int> spiralOrder(vector<vector<int> > &matrix) {
        vector<int> res;
        if (matrix.empty() || matrix[0].empty()) return res;
        int m = matrix.size(), n = matrix[0].size();
        int c = m > n ? (n + 1) / 2 : (m + 1) / 2;
        int p = m, q = n;
        for (int i = 0; i < c; ++i, p -= 2, q -= 2) {
            for (int col = i; col < i + q; ++col) 
                res.push_back(matrix[i][col]);
            for (int row = i + 1; row < i + p; ++row)
                res.push_back(matrix[row][i + q - 1]);
            if (p == 1 || q == 1) break;
            for (int col = i + q - 2; col >= i; --col)
                res.push_back(matrix[i + p - 1][col]);
            for (int row = i + p - 2; row > i; --row) 
                res.push_back(matrix[row][i]);
        }
        return res;
    }
};

  

All LeetCode Questions List 题目汇总