White sparse array of data structures

He said front:

	这部分笔记是边学习韩顺平老师的图解数据结构与算法边整理出来的,其中也加入了一些拙见,希望2019的暑假可以让自己的编程基础更加扎实。

Sparse array:

  1. concept
  2. Applications
  3. Code: a two-dimensional array to a sparse array
  4. Code: sparse array to a two-dimensional array

concept:

When most of the elements in an array is 0, or a value of the same array, the sparse array can be used to hold the array.
Save the raw data of the first line of the sparse array of rows and columns, and there are several different values, followed by a few lines provides a coordinate value in such a way to simplify the size of the array

This figure shows the left side of the original array, the sparse array using the right of the original array simplify

Applications

When using the code to represent the game board, generally used in the space 0 indicates, the use of pieces of different colors to represent different letters
Here Insert Picture Description

Code: a two-dimensional array to a sparse array

Ideas:
1. traverse the original two-dimensional array, the number of effective data SUM
2. sparseArr int sparse array can be created according to SUM [SUM +. 1] [. 3]
3. The two-dimensional array of valid data stored in the sparse array in

//1.创建原始的二维数组
//0表示没有子 1表示黑子 2表示蓝子
int chessArray1[][] = new int[11][11];
chessArray1[1][2] = 1;
chessArray1[3][4] = 2;
//输出原始的二维数组
for (int[] row : chessArray1) {
    for (int data : row) {
        System.out.printf("%d\t", data);
    }
    System.out.println();
}
System.out.println("-------------------------------------");
 /**
        * 二维数组转换为稀疏数组
        * */
        //1.先遍历二维数组得到非零值的数据
        int sum = 0;
        for (int[] item : chessArray1) {
            for (int data : item) {
                if (data != 0) {
                    sum++;
                }
            }
        }
//        System.out.println(sum);
        //创建对应的稀疏数组
        int sparseArray[][] = new int[sum + 1][3];
        //给稀疏数组赋值
        sparseArray[0][0] = 11;
        sparseArray[0][1] = 11;
        sparseArray[0][2] = sum;

        int count = 0;//第几个非零数据
        //遍历二维数组 将非零值存放到稀疏数组中
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (chessArray1[i][j] != 0) {
                    count++;
                    sparseArray[count][0] = i;
                    sparseArray[count][1] = j;
                    sparseArray[count][2] = chessArray1[i][j];
                }
            }
        }
        System.out.println("稀疏数组创建完毕");
        for (int[] row : sparseArray) {
            for (int data : row) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }
        System.out.println("-----------------------");

Code: sparse array to a two-dimensional array

Ideas:
1. First read the first line of sparse array, according to the data of the first line, creating an original two-dimensional array
2. The data is read again after a few lines of sparse arrays, and assigned to the original two-dimensional array that is can

int chessArr2[][] = new int[sparseArray[0][0]][sparseArray[0][1]];
    //恢复的二维数组
    for (int[] row : chessArr2) {
        for (int data : row) {
            System.out.printf("%d\t", data);
        }
        System.out.println();
    }
    //恢复二维数组 从第二行开始遍历
    for(int i=1;i<sparseArray.length;i++){
        chessArr2[sparseArray[i][0]][sparseArray[i][1]]=sparseArray[i][2];
    }
    System.out.println("恢复啦");
    for (int[] row : chessArr2) {
        for (int data : row) {
            System.out.printf("%d\t", data);
        }
        System.out.println();
    }
}
He published 193 original articles · won praise 70 · views 120 000 +

Guess you like

Origin blog.csdn.net/Lzinner/article/details/94553970