多维数组和矩阵—-基础训练

Java基础

浏览数:74

2019-10-8

AD:资源代下载服务

基础题一:顺时针打印二维数组

  代码:

 1 /**
 2  * 顺时针打印二维数组
 3  输入
 4  1     2     3     4
 5  5     6     7     8
 6  9     10     11     12
 7  13    14    15    16
 8  输出
 9  1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
10  */
11 public class Print2DArr {
12     public static void main(String[] args) {
13         int[][] matrix = {
14                 {1,2,3,4},
15                 {5,6,7,8},
16                 {9,10,11,12},
17                 {13,14,15,16},
18         };
19         print(matrix);
20     }
21     /**
22      * 一圈一圈的打印
23      * @param matrix
24      */
25     static void print(int [][]matrix){
26         int leftUpRow = 0,leftUpCol = 0,rightDownRow = matrix.length-1,rightDownCol = matrix[0].length-1;
27         while(leftUpRow<=rightDownRow&&leftUpCol<=rightDownCol){
28             int r = leftUpRow,c = leftUpCol;
29             // 打印上边一条边
30             while(c <= rightDownCol){
31                 System.out.print(matrix[r][c++]+" ");
32             }
33             // 恢复
34             c = rightDownCol;
35             r++;
36             // 右边的一条边
37             while(r<=rightDownRow){
38                 System.out.print(matrix[r++][c]+" ");
39             }
40             // 恢复
41             r = rightDownRow;
42             c--;
43             // 打印下面一条边
44             while(c>=leftUpCol){
45                 System.out.print(matrix[r][c--]+" ");
46             }
47             // 恢复
48             c = leftUpCol;
49             r--;
50             // 左边的一条边
51             while(r>leftUpCol){
52                 System.out.print(matrix[r--][c]+" ");
53             }
54             leftUpCol++;
55             leftUpRow++;
56             rightDownCol--;
57             rightDownRow--;
58         }
59     }
60 }

  结果:

    

基础题二:将0所在的行列清零

  代码:

import java.util.Arrays;

public class ClearZeroIn2DArr {

    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, 107, 103},
            };
        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]+"\t");
            }
            System.out.println();
        }
    }
    
    static void solve(int[][]matrix){
        int M = matrix.length;
        int N = matrix[0].length;
        // 记录哪些行出现了0
        int[]rowRecord = new int[M];
        // 记录哪些列出现了0
        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<M;row++){
            for(int col = 0;col<N;col++){
                // 当前的行或者列被标记了,这个元素就应该变为0
                if (rowRecord[row]==1||colRecord[col]==1) {
                    matrix[row][col] = 0;
                }
            }
        }
    }

}

  结果:

    

基础题三:Z形打印二维数组

  代码:

 1 /**
 2  * 1    2    3    4
 3  * 5    6    7    8
 4  * 9    10    11    12
 5  * 
 6  * Z形打印
 7  *  1-2  3 - 4
 8  *   /  /   /
 9  *  5  6   7   8
10  *  | /   /  / |
11  *  9   10--11 12
12  * 
13  * 输出:1 2 5 9 6 3 4 7 10 11 8 12 
14  */
15 public class PrintMatrixZig {
16 
17     public static void main(String[] args) {
18         int[][] matrix = {
19                 {1, 2, 3, 4},
20                 {5, 6, 7, 8},
21                 {9, 10, 11, 12},
22                 // {13, 14, 15, 16},
23             };
24         print(matrix);
25     }
26     
27     static void print(int [][]matrix){
28         int r = 0,m = matrix.length;
29         int c = 0,n = matrix[0].length;
30         boolean l2r = true;  // 从左到右或者从右到左
31         while(r<m&&c<n){
32             // 从左下到右上的斜线
33             if (l2r) {
34                 System.out.print(matrix[r][c]+" ");
35                 // 现在第一行,列未到边界,这是只能向右走
36                 if (r == 0&&c<n-1) {
37                     l2r = !l2r;  // 方向切换
38                     c++;
39                     continue;
40                 }else if (r>0&&c==n-1) {  // 现在在最后一列,只能向下走
41                     l2r = !l2r;
42                     r++;
43                     continue;
44                 }else {  // 继续走上坡
45                     r--;
46                     c++;
47                 }
48             }else { // 反,走下坡
49                 System.out.print(matrix[r][c]+" ");
50                 if (c==0&&r<m-1) { // 走到第一列,只能往下走
51                     l2r = !l2r;
52                     r++;
53                     continue;
54                 }else if (r == m-1) { // 到最后一行,只能往右走
55                     l2r = !l2r;
56                     c++;
57                     continue;
58                 }else {
59                     r++;
60                     c--;
61                 }
62             }
63         }
64     }
65 
66 }

  结果:

    

  

作者:|旧市拾荒|