• 【LeetCode-2326】螺旋矩阵IV


    LeetCode 每日一题(第 300 场周赛第 2 题)
    有没有人组团一起刷题的?
    力扣刚起步,才刷了 10 道题,目标一年刷到 1000 题

    在这里插入图片描述
    刚开始刷算法题,周赛成绩惨不忍睹,成绩就不贴了,一起组团好好研究一下题有没有?

    题目链接

    https://leetcode.cn/problems/spiral-matrix-iv/

    题目描述

    螺旋矩阵IV


    给你两个整数:m 和 n ,表示矩阵的维数。
    另给你一个整数链表的头节点 head 。
    请你生成一个大小为 m x n 的螺旋矩阵,矩阵包含链表中的所有整数。链表中的整数从矩阵 左上角 开始、顺时针 按 螺旋 顺序填充。如果还存在剩余的空格,则用 -1 填充。
    返回生成的矩阵。


    示例 1:
    在这里插入图片描述
    输入:m = 3, n = 5, head = [3,0,2,6,8,1,7,9,4,2,5,5,0]
    输出:[[3,0,2,6,8],[5,0,-1,-1,1],[5,2,4,9,7]]
    解释:上图展示了链表中的整数在矩阵中是如何排布的。
    注意,矩阵中剩下的空格用 -1 填充。

    分析

    此题重点在于上下左右边界的移动。

    1. 先从左至右,填充完之后,上边界下移
    2. 再从上至下,填充完之后,右边界左移
    3. 再从右至左,填充完之后,下边界上移
    4. 再从下至上,填充完之后,左边界右移
    5. 最后注意三种特殊情况的边界判定:
      • m = 1 时,只有横向左至右移动,注意不要让他死循环
      • n = 1 时,只有纵向上至下移动
      • m = 1 且 n = 1 时

    题解

    class Solution {
        public int[][] spiralMatrix(int m, int n, ListNode head) {
            // 1. 创建新数组,默认全部填充 -1
            int [][] arr = new int[m][n];
            for(int i= 0;i<m;i++){
                for(int j=0;j<n;j++){
                    arr[i][j]=-1;
                }
            }
    
            // 2. 定义边界
            int left=0, top=0,right=n-1,bottom=m-1;
    
            // 3. 遍历链表,填充数据(结束条件: 左边界大于右边界或者上边界大于下边界)
            while(!(right<left||bottom<top)){
                // 3.1 →
                if(left <=right && top <= bottom) {
                    // 如果top大于bottom,则说明最后一个横行已经填充完成,结束,如果不结束会又反着 ← 回来
                    for(int i =left;i<=right;i++){
                        if(head==null) return arr;
                        arr[top][i]=head.val;
                        head=head.next;
                    }
                    // 上边界下移
                    top++;
                }
                
                // 3.2 ↓
                if(top <= bottom&&left<=right) {
                    for(int i =top;i<=bottom;i++){
                        if(head==null) return arr;
                        arr[i][right]=head.val;
                        head=head.next;
                    }
                    // 右边界左移
                    right--;
                }
                
                // 3.3 ←
                if(left <=right && top <= bottom) {
                    for(int i =right;i>=left;i--){
                        if(head==null) return arr;
                        arr[bottom][i]=head.val;
                        head=head.next;
                    }
                    // 下边界上移
                    bottom--;
                }
                
                // 3.4 ↑
                if(top <= bottom&&left<=right) {
                    for(int i =bottom;i>=top;i--){
                        if(head==null) return arr;
                        arr[i][left]=head.val;
                        head=head.next;
                    }
                    // 左边界右移
                    left++;
                }
            }
    
    
            return arr;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65

    运行结果

    在这里插入图片描述

  • 相关阅读:
    提升团队能力的真正利器不是培训而是复盘,
    微信小程序--下拉选择框组件封装,可CV直接使用
    【分享】“飞鹅打印机“ 在集简云平台集成应用的常见问题与解决方案
    LeetCode 731. My Calendar II【设计,有序映射,差分;线段树】中等
    Linux系列---【查看mac地址】
    383. 赎金信
    数据结构与算法之美读书笔记15
    Creaform形创HandySCAN MAX三维扫描仪大型零部件尺寸测量设备
    java毕业设计家政客户服务管理系统mybatis+源码+调试部署+系统+数据库+lw
    lammps提取和保存data文件中力场参数的技巧
  • 原文地址:https://blog.csdn.net/shaotaiban1097/article/details/125611170