当前位置:网站首页>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 :


边栏推荐
- 浅谈三点建议为所有已经毕业和终将毕业的同学
- 亚马逊aws数据湖工作之坑1
- 浏览器原理思维导图
- 【每日一题】写一个函数,判断一个字符串是否为另外一个字符串旋转之后的字符串。
- Golang -- map capacity expansion mechanism (including source code)
- Learn about various joins in SQL and their differences
- Ros2 --- lifecycle node summary
- Contest3147 - game 38 of 2021 Freshmen's personal training match_ F: Polyhedral dice
- In depth understanding of JUC concurrency (I) what is JUC
- New version of dedecms collection and release plug-in tutorial tool
猜你喜欢

Replace Django database with MySQL (attributeerror: 'STR' object has no attribute 'decode')

深入了解JUC并发(一)什么是JUC

Don't use the new WP collection. Don't use WordPress collection without update

网络相关知识(硬件工程师)

From design delivery to development, easy and efficient!

VLAN experiment of switching technology

Contest3147 - game 38 of 2021 Freshmen's personal training match_ G: Flower bed

来自读者们的 I/O 观后感|有奖征集获奖名单

Redis——热点key问题

广告业务Bug复盘总结
随机推荐
IDEA公布全新默认UI,太清爽了(内含申请链接)
sudo提权
介绍两款代码自动生成器,帮助提升工作效率
Pbootcms collection and warehousing tutorial quick collection release
栈(线性结构)
In depth understanding of JUC concurrency (I) what is JUC
Flutter hybrid development: develop a simple quick start framework | developers say · dtalk
Reading classic literature -- Suma++
Find the highest value of the current element Z-index of the page
WLAN相关知识点总结
Redis——大Key問題
TensorRT的数据格式定义详解
Invalid operation: Load into table ‘sources_ orderdata‘ failed. Check ‘stl_ load_ errors‘ system table
分布式事务 :可靠消息最终一致性方案
Data playback partner rviz+plotjuggler
Mech 3002 explanation
利用NVIDIA GPU将Minecraft场景渲染成真实场景
Hydration failed because the initial UI does not match what was rendered on the server.问题原因之一
阿里云MFA绑定Chrome浏览器
TensorRT的功能