leetcode--顺时针打印矩阵

本文详细解析了LeetCode面试题29中顺时针打印矩阵的经典算法,通过设定边界,模仿打印顺序,实现从外向里的顺时针打印。提供JAVA与Python代码示例,时间复杂度为O(mn),空间复杂度为O(1)。

 题目来自LeetCode,链接:面试题29. 顺时针打印矩阵。具体描述为:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。
 示例1:

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

 示例2:

输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
输出:[1,2,3,4,8,12,11,10,9,5,6,7]

 蛮经典的题目,记得本科那会参加一个笔试就做过这道题。做法也比较浅显,就是模仿打印顺序,先往右,再往下,接着往左最后往上,打印这个过程中遇到的数。需要注意的是我们需要设置四个边界限制我们遍历的边界,然后我们在往右走的时候其实就是在上边界上走,在往下走的时候其实就是在右边界上走,其他两种情况类似。然后改变方向的同时也需要缩小范围,也就是需要改变边界,比如从往右走变为往下走的时候就需要将上边界下移。时间复杂度为 O ( m n ) O(mn) O(mn),空间复杂度为 O ( 1 ) O(1) O(1)

 JAVA版代码如下:

class Solution {
    public int[] spiralOrder(int[][] matrix) {
        int row = matrix.length;
        if (row == 0) {
            return new int[0];
        }
        int col = matrix[0].length;
        int totalCount = row * col;
        int[] result = new int[totalCount];
        if (totalCount == 0) {
            return result;
        }
        int left = 0, right = col - 1, top = 0, bottom = row - 1;
        int idx = 0;
        while (idx < totalCount) {
            for (int j = left; j <= right; ++j) {
                result[idx++] = matrix[top][j];
            }
            ++top;
            if (idx >= totalCount) {
                break;
            }
            for (int i = top; i <= bottom; ++i) {
                result[idx++] = matrix[i][right];
            }
            --right;
            if (idx >= totalCount) {
                break;
            }
            for (int j = right; j >= left; --j) {
                result[idx++] = matrix[bottom][j];
            }
            --bottom;
            if (idx >= totalCount) {
                break;
            }
            for (int i = bottom; i >= top; --i) {
                result[idx++] = matrix[i][left];
            }
            ++left;
        }
        return result;
    }
}

 提交结果如下:


 Python版代码如下:

class Solution:
    def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
        row = len(matrix)
        if row == 0:
            return []
        col = len(matrix[0])
        if col == 0:
            return []
        result = []
        top, bottom = 0, row - 1
        left, right = 0, len(matrix[0]) - 1
        count = row * col
        idx = 0
        while idx < count:
            for j in range(left, right + 1):
                result.append(matrix[top][j])
                idx += 1
            if idx >= count:
                break
            top += 1
            for i in range(top, bottom + 1):
                result.append(matrix[i][right])
                idx += 1
            if idx >= count:
                break
            right -= 1
            for j in range(right, left - 1, -1):
                result.append(matrix[bottom][j])
                idx += 1
            if idx >= count:
                break
            bottom -= 1
            for i in range(bottom, top - 1, -1):
                result.append(matrix[i][left])
                idx += 1
            left += 1
        return result
        

 提交结果如下:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值