Saburo data structure algorithm study notes: sparse array

Data structure algorithm study notes: sparse array

Algorithm : Sparse array
Purpose : For a bunch of data, most of the data in it is the same, only a few of them are different. If it is stored directly, it will cause a waste of space. For this reason, a sparse array is used to store the data.
Idea : We can create another array and store the original array The space structure of, and the position and value of each different value, and then store this array in the original array. When accessing the original array, restore the original array data based on this array. The diagram is as follows:
Algorithm idea

I can see on the picture that a new sparse array is created, which stores related data: the number of rows and columns of the original array, the number of different values; the position and value of each different value in the original array.
At this point, I believe that everyone already knows how to restore. According to the sparse array, the conditional loop is very simple to generate the original array.
Reference Java algorithm source code

/*
* Author:sanlang
* time:2020.10.24
* function:稀疏数组
*
* */
public class SparseArray {
	public static void main(String[] args) {
//创建一个原始的二维数组 11 * 11  0: 表示没有棋子, 1 表示 黑子 2 表蓝子 
		 int chessArr1[][] = new int[11][11]; chessArr1[1][2] = 1;
		 chessArr1[2][3] = 2; chessArr1[4][5] = 2;
		 //输出原始的二维数组
        System.out.println("原始的二维数组~~");
         for (int[] row : chessArr1) {
         for (int data : row) { System.out.printf("%d\t", data);
         }
        System.out.println();
        }
//将二维数组 转 稀疏数组的思想
//1. 先遍历二维数组 得到非 0 数据的个数 
	int sum = 0;
	for (int i = 0; i < 11; i++) {
	for (int j = 0; j < 11; j++) {
	if (chessArr1[i][j] != 0) { 
			sum++;}
	}
}
//2. 创建对应的稀疏数组
int sparseArr[][] = new int[sum + 1][3]; 
// 给稀疏数组赋值
sparseArr[0][0] = 11; sparseArr[0][1] = 11; sparseArr[0][2] = sum;
//遍历二维数组,将非 0 的值存放到 sparseArr 中
 int count = 0;
//count 用于记录是第几个非 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();
//将稀疏数组 --》 恢复成 原始的二维数组
/*
*	1. 先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组,
	比如上面的	chessArr2 = int[11][11]
	2. 在读取稀疏数组后几行的数据,并赋给 原始的二维数组 即可.
*/
//1. 先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组
int chessArr2[][] = new int[sparseArr[0][0]][sparseArr[0][1]];
//2. 在读取稀疏数组后几行的数据(从第二行开始),并赋给 原始的二维数组 即可
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();
}
}
}

Code result
Insert picture description here

Insert picture description here

OK, come to the algorithm problem every day and learn slowly. If you don’t
know how to leave a comment, please follow me.

Guess you like

Origin blog.csdn.net/m0_51684972/article/details/109264301