• 【算法练习】数组操作


    二维数组顺时针旋转

    解决思路

    1. 创建新数组,将原数组的数据按照一定的顺序放入新数组中。
    2. 如果是顺时针的话,原数组中第一排的元素是应该放在新数组最后一列中的。

    Java实现

    public class RotateOne {
    
        public static void main(String[] args) {
            int[][] A = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
            int n = 3;
            int[][] B = new int[n][n];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    B[j][n - 1 - i] = A[i][j];
                }
            }
            ArrayUtil.printArray(B);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    二维数组逆时针旋转

    解决思路

    1. 逆时针的话,原数组第一排的元素放在新数组的第一列

    Java实现

    public class RotateTwo {
    
        public static void main(String[] args) {
            int[][] A = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
            int n = 3;
            int[][] B = new int[n][n];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    B[n - 1 - j][i] = A[i][j];
                    //A[0][0]-->B[2][0]
                    //A[0][1]-->B[1][0]
                }
            }
            ArrayUtil.printArray(B);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    螺旋矩阵

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

    解决思路

    1. 设定数组的边界,并且不断的更新边界值。
    2. 跳出循环的条件,当更新边界值不满足条件的时候,跳出循环。

    Java实现

    class Solution_LC54_II {
        public List<Integer> spiralOrder(int[][] matrix) {
            List<Integer> res = new ArrayList<>();
            if (matrix.length == 0) {
                return res;
            }
    
            int top = 0, bottom = matrix.length - 1, left = 0, right = matrix[0].length - 1;
            while (true) {
                for (int i = left; i <= right; i++) {
                    res.add(matrix[top][i]);
                }
                if (++top > bottom) {
                    break;
                }
                for (int i = top; i <= bottom; i++) {
                    res.add(matrix[i][right]);
                }
                if (--right < left) {
                    break;
                }
                for (int i = right; i >= left; i--) {
                    res.add(matrix[bottom][i]);
                }
                if (--bottom < top) {
                    break;
                }
                for (int i = bottom; i >= top; i--) {
                    res.add(matrix[i][left]);
                }
                if (++left > right) {
                    break;
                }
            }
            return res;
        }
    }
    
    • 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

    在这里插入图片描述

  • 相关阅读:
    低代码开发:助企构建数字化应用平台
    java任务跟踪系统
    单调队列代码模板
    记一次克隆笔记本的Window 10硬盘到新的SSD的经验
    开发智能硬件过程中需要掌握的方法之经典
    数据库练习丽丽
    基于java点播影院运营系统计算机毕业设计源码+系统+lw文档+mysql数据库+调试部署
    JS单选框默认选中样式修改,为白色背景中心有黑色小圆点的样式
    Vue3+Ts+Vite 项目搭建&项目说明
    一文入门MyBatis
  • 原文地址:https://blog.csdn.net/qq_42985872/article/details/134526619