When an arrayMost of the data are 0 (both the same value) , You can use a sparse array to hold the array
Compression memory (valid data ready) You can save storage space to avoid unnecessary waste of resources
Storage (non-chain store):
0
1
2
Raw array lines
Raw array of column
The number of valid data
Effective data row
Where valid data column
valid data
–
–
–
word description
The first line of original data is storedTotal number of lines, the total number of columns, the total number of valid data
Next each row storageValid data row, column, and where the specific value
Icon original array:
0
1
2
3
4
0
0
1
0
2
0
1
0
0
0
2
0
2
1
1
0
0
0
3
0
0
0
2
0
Sparse array:
0 (Number of lines)
1 (Number of columns)
2 (valid data)
0
5 (Total number of rows)
4 (Total number of columns)
6 (The number of valid data)
1
0
1
1
2
0
3
2
3
1
3
2
4
1
0
1
5
1
1
1
6
2
4
2
Code:
Two-dimensional array is converted to a sparse array
Get the original arrayRanks number
Get the original array traversalThe number of valid data
Create a sparse array and sparse array assignment (1) on the first line of the sparse array assignment (the original arrayTotal number of lines, the total number of columns, the total number of valid data) (2) traversing the original arrayPosition corresponding to valid data andInto the sparse array
Test data of the original matrix: 00000000000 00000100000 00000010000 00000020000 00000000000 00000000100 00000000001 00000000000 00000000000 00002000000 00000200000