[算法很美打卡] 多维数组篇 (打卡第一天)

顺时针打印二维数组

在这里插入图片描述

package 每日算法学习打卡.算法打卡.七月份.七月二十六号;

public class test1 {
    public static void main(String[] args) {
        int[][] matrix = {
                {1,2},
                {5,6},
                {9,10},
                {13,14},
        };
        print(matrix);
    }
    static void print(int[][] matrix){
        int leftUpRow =0,leftUpCol =0,rightDownRow = matrix.length-1,rightDownCol = matrix[0].length-1;
        while(leftUpRow<=rightDownRow && leftUpCol <= rightDownCol){
            int r = leftUpRow,c = leftUpCol;
            while(c<=rightDownCol){
                System.out.print(matrix[r][c++]+" ");
            }
            r++;
            c = rightDownCol;
            while(r<=rightDownRow){
                System.out.print(matrix[r++][c]+" ");
            }
            c--;
            r= rightDownRow;
            while(c>=leftUpCol){
                System.out.print(matrix[r][c--]+" ");
            }
            r--;
            c = leftUpCol;
            while(r>leftUpRow){
                System.out.print(matrix[r--][c]+" ");
            }
            leftUpCol++;
            leftUpRow++;
            rightDownCol--;
            rightDownRow--;
        }

    }
}

0所在的行列清零

在这里插入图片描述

package 每日算法学习打卡.算法打卡.七月份.七月二十六号;

public class test2 {
    public static void main(String[] args) {
        int[][] matrix = {
                {1,2,3,4,100},
                {5,6,7,0,101},
                {9,0,11,12,102},
                {13,14,15,16,103},
                {104,105,106,106,108},
        };
        solve(matrix);
        for(int i =0;i<matrix.length;i++){
            for(int j =0;j<matrix[0].length;j++){
                System.out.print(matrix[i][j]+" ");
            }
            System.out.println();
        }
    }
    static void solve(int[][] matrix){
        //使用辅助数组来进行测试
        int M = matrix.length;
        int N = matrix.length;
        //记录哪些行出现了0
        int[] rowRecord = new int[M];
        int[] colRecord = new int[N];
        for(int i =0;i<M;i++){
            for(int j =0;j<N;j++){
                if(matrix[i][j] == 0){
                    rowRecord[i] =1;
                    colRecord[j] =1;
                }
            }
        }
        for(int row = 0;row < N;row++){
            for(int col = 0;col<M;col++){
                if(rowRecord[row] ==1 || colRecord[col] == 1){
                    matrix[row][col] =0;
                }
            }
        }
    }

}

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

万物皆可der

感谢支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值