当前位置:网站首页>Sparse array (nonlinear structure)
Sparse array (nonlinear structure)
2022-07-02 06:22:00 【I think starfish_ ninety-eight】
When in an array Most elements are 0, Or for Array of the same value when , You can use a sparse array to hold the array .
The transformation process
Gobang board > Two dimensional array > Sparse array > Storage
The illustration
- 11*11 Two dimensional array of , among 1 The location of the for [1] [2],2 The location of the for [2] [3]
- 3*3 Sparse two-dimensional array of , The first row of data is Row column value and Number of non-zero values , The data in the last few lines is Stored non-zero value

Code
Pay attention to the ideas of the notes ~
package com.atguigu.sparsearray;
//author qij
public class SparseArray {
public static void main(String[] args) {
// Create an original two-dimensional array 11 * 11
int chessArr1[][] = new int[11][11];
chessArr1[1][2] = 1;
chessArr1[2][3] = 2;
chessArr1[4][5] = 2;
// Output the original two-dimensional array
System.out.println(" The original two-dimensional array ~~");
for (int[] row : chessArr1) {
for (int data : row) {
System.out.printf("%d\t", data);
}
System.out.println();
}
// Put two-dimensional array turn Sparse array
// 1. First, traverse the two-dimensional array Get the blame 0 Number of data
int sum = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (chessArr1[i][j] != 0) {
sum++;
}
}
}
//2. Create the corresponding sparse array
int sparseArr[][] = new int[sum + 1][3];
//3. Assign a value to the first row of the sparse array
sparseArr[0][0] = 11;
sparseArr[0][1] = 11;
sparseArr[0][2] = sum;
//4. Traversing a two-dimensional array , Will not 0 The value of is stored in sparseArr in
int count = 0; //count It's the number one non 0 data
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];
}
}
}
// Output
System.out.println();
System.out.println(" Get the sparse array as ~~~~");
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();
// Sparse array recovery The original two-dimensional array
//1. First read the first row of the sparse array , According to the data in the first line , Create the original two-dimensional array
int chessArr2[][] = new int[sparseArr[0][0]][sparseArr[0][1]];
//2. After reading the sparse array a few lines of data ( Start with the second line ), And give it to The original two-dimensional array that will do
for(int i = 1; i < sparseArr.length; i++) {
chessArr2[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
}
// Output
System.out.println();
System.out.println(" Recovered 2D array ");
for (int[] row : chessArr2) {
for (int data : row) {
System.out.printf("%d\t", data);
}
System.out.println();
}
}
}
Output results :


边栏推荐
- Contest3147 - game 38 of 2021 Freshmen's personal training match_ F: Polyhedral dice
- Invalid operation: Load into table ‘sources_ orderdata‘ failed. Check ‘stl_ load_ errors‘ system table
- ROS create workspace
- CUDA中的异步数据拷贝
- LeetCode 90. 子集 II
- Step by step | help you easily submit Google play data security form
- Data playback partner rviz+plotjuggler
- 谷歌出海创业加速器报名倒计时 3 天,创业人闯关指南提前收藏!
- On Web server
- Compte à rebours de 3 jours pour l'inscription à l'accélérateur de démarrage Google Sea, Guide de démarrage collecté à l'avance!
猜你喜欢

数据科学【八】:SVD(一)

BGP报文详细解释

深入了解JUC并发(二)并发理论

Community theory | kotlin flow's principle and design philosophy

Google play academy team PK competition, official start!

VRRP之监视上行链路

sudo提权

Compte à rebours de 3 jours pour l'inscription à l'accélérateur de démarrage Google Sea, Guide de démarrage collecté à l'avance!

Detailed explanation of BGP message

Browser principle mind map
随机推荐
Arduino Wire 库使用
网络相关知识(硬件工程师)
LeetCode 90. 子集 II
注解和反射详解以及运用
CUDA中的动态全局内存分配和操作
Talking about MySQL database
Golang--map扩容机制(含源码)
Cglib代理-代码增强测试
TensorRT中的循环
Invalid operation: Load into table ‘sources_orderdata‘ failed. Check ‘stl_load_errors‘ system table
社区说|Kotlin Flow 的原理与设计哲学
LeetCode 283. 移动零
Is there a really free applet?
IDEA公布全新默认UI,太清爽了(内含申请链接)
LeetCode 83. Delete duplicate elements in the sorting linked list
广告业务Bug复盘总结
Classic literature reading -- deformable Detr
Ros2 --- lifecycle node summary
加密压缩文件解密技巧
Replace Django database with MySQL (attributeerror: 'STR' object has no attribute 'decode')