当前位置:网站首页>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 :
边栏推荐
- 穀歌出海創業加速器報名倒計時 3 天,創業人闖關指南提前收藏!
- Invalid operation: Load into table ‘sources_orderdata‘ failed. Check ‘stl_load_errors‘ system table
- Contest3147 - game 38 of 2021 Freshmen's personal training match_ 1: Maximum palindromes
- 来自读者们的 I/O 观后感|有奖征集获奖名单
- Redis---1. Data structure characteristics and operation
- Monitoring uplink of VRRP
- 链表(线性结构)
- 代码技巧——Controller参数注解@RequestParam
- Browser principle mind map
- LeetCode 47. Full arrangement II
猜你喜欢
Contest3147 - game 38 of 2021 Freshmen's personal training match_ F: Polyhedral dice
I/o impressions from readers | prize collection winners list
Replace Django database with MySQL (attributeerror: 'STR' object has no attribute 'decode')
Data science [viii]: SVD (I)
IDEA公布全新默认UI,太清爽了(内含申请链接)
In depth understanding of JUC concurrency (I) what is JUC
ShardingSphere-JDBC篇
Amazon AWS data Lake Work Pit 1
LeetCode 90. Subset II
实习生跑路留了一个大坑,搞出2个线上问题,我被坑惨了
随机推荐
Redis——Cluster数据分布算法&哈希槽
Hydration failed because the initial UI does not match what was rendered on the server.问题原因之一
利用NVIDIA GPU将Minecraft场景渲染成真实场景
Redis---1.数据结构特点与操作
MySQL的10大经典错误
深入学习JVM底层(四):类文件结构
Step by step | help you easily submit Google play data security form
TensorRT的数据格式定义详解
Zhuanzhuanben - LAN construction - Notes
Lucene Basics
Contest3147 - game 38 of 2021 Freshmen's personal training match_ 1: Maximum palindromes
LeetCode 47. Full arrangement II
LeetCode 283. 移动零
MySQL的10大經典錯誤
LeetCode 39. 组合总和
Does the assignment of Boolean types such as tag attribute disabled selected checked not take effect?
Amazon AWS data Lake Work Pit 1
Contest3147 - game 38 of 2021 Freshmen's personal training match_ G: Flower bed
深入了解JUC并发(一)什么是JUC
Sumo tutorial Hello World