Sparse array implementation

Foreword

The text has been included in my GitHub repository. Welcome to Star: github.com/bin39232820 ... The
best time to plant a tree was ten years ago, followed by now.
I know that many people do n’t play qq anymore, but nostalgia, welcome to join six Pulse Excalibur Java rookie learning group, group chat number: 549684836 encourage everyone to blog on the road to technology

Chatter

A very simple question, is similar to compression, decompression to find a way to save space

topic

As shown in a chess board, how should we keep this record of playing chess?

In fact, we can turn him into an array to represent, as shown below.

We can see that there are many 0 elements in the original array, occupying a lot of space, then our purpose is to remove these 0 elements, and then compress this two-dimensional array to turn it into a sparse array. As follows

The originally occupied space is 11x11. Now it is 3x3. Is it a lot of reduced storage? So how do we implement it in the code?

Problem-solving ideas

The idea of ​​converting two-dimensional array to sparse array

  1. Traverse the original two-dimensional array to get the number of valid data sum
  2. According to sum, you can create a sparse array sparseArr int [sum + 1] [3]
  3. Store the effective data of the two-dimensional array into a sparse array

The idea of ​​converting sparse array to original two-dimensional array

  1. First read the first row of the sparse array, and create an original two-dimensional array based on the data in the first row, such as chessArr2 = int above [11] [11]
  2. Read the data in the last few lines of the sparse array and assign it to the original two-dimensional array

Code

package com.code.array;

/**
 * @author 小六六
 * @version 1.0
 * @date 2020/4/19 13:41
 */
public class SparseArray {

    public static void main(String[] args) {

        // 创建一个原始的二维数组 11 * 11
        int chessArr1[][] = new int[11][11];
        chessArr1[1][2] = 1;
        chessArr1[2][3] = 2;

        // 输出原始的二维数组
        System.out.println("原始的二维数组");
        for (int[] row : chessArr1) {
            for (int data : row) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }

        // 将二维数组 转 稀疏数组的思
        //先遍历二维数组 得到非0数据的个数
        int sum = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (chessArr1[i][j] != 0) {
                    sum++;
                }
            }
        }

        //创建对应的稀疏数组

        int sparseArr [][]=new int[sum+1][3];

        //给稀疏数组赋值,稀疏数组的第一行 表示的是 原数组的大小 和不为0的个数
        sparseArr[0][0]=11;
        sparseArr[0][1]=11;
        sparseArr[0][2]=sum;
        int count=0;
        //把补位0的位置和值赋值给稀疏数组 从第二行开始
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (chessArr1[i][j] != 0) {
                    count++;
                    sparseArr[count][0]=i;
                    sparseArr[count][1]=j;
                    sparseArr[count][2]= chessArr1[i][j];                }
            }
        }

        // 输出稀疏数组的形式
        System.out.println();
        System.out.println("得到稀疏数组为~~~~");
        for (int i = 0; i < sparseArr.length; i++) {
            System.out.printf("%d\t%d\t%d\t\n", sparseArr[i][0], sparseArr[i][1], sparseArr[i][2]);
        }
        System.out.println();


        //将稀疏数组 --》 恢复成 原始的二维数组

        //先确定原二维数组的大小
        int chessArr2[] [] =new int[sparseArr[0][0]][sparseArr[0][1]];
        //在读取稀疏数组后几行的数据(从第二行开始),并赋给 原始的二维数组 所以遍历就从第二行 1开始
        for (int i=1;i<sparseArr.length;i++){
            chessArr2[sparseArr[i][0]][sparseArr[i][1]]=sparseArr[i][2];
        }

        // 输出恢复后的二维数组
        System.out.println();
        System.out.println("恢复后的二维数组");

        for (int[] row : chessArr2) {
            for (int data : row) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }


    }


}

复制代码

end

A simple topic, I hope you like it.

Daily praise

Alright everyone, the above is the entire content of this article, you can see the people here, all are true fans .

Creation is not easy. Your support and recognition is my biggest motivation for creation. See you in the next article.

Liumai Excalibur | Text [Original] If there are any errors in this blog, please criticize and instruct, thank you!

Guess you like

Origin juejin.im/post/5e9b2004e51d4546cf778d8b