解题思路

这是一个顺时针打印矩阵的问题。可以通过定义四个边界,按照右、下、左、上的顺序遍历矩阵。

关键点:

  1. 定义四个边界:上、下、左、右
  2. 按照顺时针方向遍历
  3. 每打印完一条边,更新相应的边界
  4. 注意边界条件的处理

算法步骤:

  1. 初始化四个边界值
  2. 按照右、下、左、上的顺序遍历
  3. 每次遍历完更新边界
  4. 直到所有元素都被访问

代码

class Printer {
public:
    vector<int> clockwisePrint(vector<vector<int> >& mat, int n, int m) {
        if (mat.empty() || n == 0 || m == 0) {
            return vector<int>();
        }
        
        vector<int> result;
        int top = 0, bottom = n - 1;
        int left = 0, right = m - 1;
        
        while (left <= right && top <= bottom) {
            // 从左到右
            for (int j = left; j <= right; j++) {
                result.push_back(mat[top][j]);
            }
            top++;
            
            // 从上到下
            for (int i = top; i <= bottom; i++) {
                result.push_back(mat[i][right]);
            }
            right--;
            
            // 从右到左
            if (top <= bottom) {
                for (int j = right; j >= left; j--) {
                    result.push_back(mat[bottom][j]);
                }
                bottom--;
            }
            
            // 从下到上
            if (left <= right) {
                for (int i = bottom; i >= top; i--) {
                    result.push_back(mat[i][left]);
                }
                left++;
            }
        }
        
        return result;
    }
};
import java.util.*;

public class Printer {
    public int[] clockwisePrint(int[][] mat, int n, int m) {
        if (mat == null || n == 0 || m == 0) {
            return new int[0];
        }
        
        int[] result = new int[n * m];
        int index = 0;
        
        int top = 0, bottom = n - 1;
        int left = 0, right = m - 1;
        
        while (left <= right && top <= bottom) {
            // 从左到右
            for (int j = left; j <= right; j++) {
                result[index++] = mat[top][j];
            }
            top++;
            
            // 从上到下
            for (int i = top; i <= bottom; i++) {
                result[index++] = mat[i][right];
            }
            right--;
            
            // 从右到左
            if (top <= bottom) {
                for (int j = right; j >= left; j--) {
                    result[index++] = mat[bottom][j];
                }
                bottom--;
            }
            
            // 从下到上
            if (left <= right) {
                for (int i = bottom; i >= top; i--) {
                    result[index++] = mat[i][left];
                }
                left++;
            }
        }
        
        return result;
    }
}
#!/usr/bin/env python
# -*- coding: utf-8 -*-

class Solution:
    def clockwisePrint(self, mat, n, m):
        # 处理边界情况
        if not mat or n == 0 or m == 0:
            return []
        
        result = []
        top, bottom = 0, n - 1
        left, right = 0, m - 1
        
        while left <= right and top <= bottom:
            # 从左到右
            for j in range(left, right + 1):
                result.append(mat[top][j])
            top += 1
            
            # 从上到下
            for i in range(top, bottom + 1):
                result.append(mat[i][right])
            right -= 1
            
            # 从右到左
            if top <= bottom:
                for j in range(right, left - 1, -1):
                    result.append(mat[bottom][j])
                bottom -= 1
            
            # 从下到上
            if left <= right:
                for i in range(bottom, top - 1, -1):
                    result.append(mat[i][left])
                left += 1
        
        return result

算法及复杂度

  • 算法:模拟
  • 时间复杂度:,每个元素访问一次
  • 空间复杂度:,存储结果数组