快手高频编程考题:螺旋矩阵(中等)

题目描述

给你一个 m 行 n 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。

示例 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]

提示:

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 10
  • -100 <= matrix[i][j] <= 100

解题思路

  • 初始化边界:定义四个边界 topbottomleftright,它们分别代表当前矩阵的上下左右边界。初始值分别为矩阵的四个边界。

  • 遍历矩阵

    从左到右:遍历当前 top 行,将元素添加到结果列表中,然后 top 向下移动;从上到下:遍历当前 right 列,将元素添加到结果列表中,然后 right 向左移动;从右到左:如果 top 行和 bottom 行还有未处理的部分,遍历当前 bottom 行,将元素添加到结果列表中,然后 bottom 向上移动;从下到上:如果 left 列和 right 列还有未处理的部分,遍历当前 left 列,将元素添加到结果列表中,然后 left 向右移动。
  • 更新边界:每完成一个方向的遍历后,更新相应的边界值,缩小螺旋矩阵的范围。

复杂度分析

  • 时间复杂度:O(m * n),因为每个元素被访问一次。
  • 空间复杂度:O(m * n),用于存储结果列表。

代码实现

package org.zyf.javabasic.letcode.hot100.matrix;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: zyfboot-javabasic
 * @description: 螺旋矩阵​
 * @author: zhangyanfeng
 * @create: 2024-08-21 22:53
 **/
public class SpiralOrderSolution {
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> result = new ArrayList<>();

        // 获取矩阵的行数和列数
        int m = matrix.length;
        int n = matrix[0].length;

        // 定义四个边界
        int top = 0, bottom = m - 1;
        int left = 0, right = n - 1;

        // 只要还有未处理的区域
        while (top <= bottom && left <= right) {
            // 从左到右遍历当前上边界
            for (int j = left; j <= right; j++) {
                result.add(matrix[top][j]);
            }
            top++; // 上边界向下移动

            // 从上到下遍历当前右边界
            for (int i = top; i <= bottom; i++) {
                result.add(matrix[i][right]);
            }
            right--; // 右边界向左移动

            // 确保当前行还有未处理的部分
            if (top <= bottom) {
                // 从右到左遍历当前下边界
                for (int j = right; j >= left; j--) {
                    result.add(matrix[bottom][j]);
                }
                bottom--; // 下边界向上移动
            }

            // 确保当前列还有未处理的部分
            if (left <= right) {
                // 从下到上遍历当前左边界
                for (int i = bottom; i >= top; i--) {
                    result.add(matrix[i][left]);
                }
                left++; // 左边界向右移动
            }
        }

        return result;
    }

    public static void main(String[] args) {
        SpiralOrderSolution solution = new SpiralOrderSolution();

        // 测试用例 1
        int[][] matrix1 = {
                {1, 2, 3},
                {4, 5, 6},
                {7, 8, 9}
        };
        System.out.println(solution.spiralOrder(matrix1)); // 输出: [1,2,3,6,9,8,7,4,5]

        // 测试用例 2
        int[][] matrix2 = {
                {1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 10, 11, 12}
        };
        System.out.println(solution.spiralOrder(matrix2)); // 输出: [1,2,3,4,8,12,11,10,9,5,6,7]
    }
}

具体可参考:https://zyfcodes.blog.csdn.net/article/details/141401712

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值