当前位置:网站首页>稀疏数组(非线性结构)
稀疏数组(非线性结构)
2022-07-02 06:09:00 【我觉得海星_98】
当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组。
转化过程
五子棋棋盘 > 二维数组 > 稀疏数组 > 存储
图解
- 11*11的二维数组,其中1的位置为[1] [2],2的位置为[2] [3]
- 3*3的稀疏二维数组,其中首行数据为 行列值 和 非零值的个数,后几行数据为 存储的非零值
代码
注意看注释的思路讲解~
package com.atguigu.sparsearray;
//author qij
public class SparseArray {
public static void main(String[] args) {
// 创建一个原始的二维数组 11 * 11
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];
//3.给稀疏数组的首行数据赋值
sparseArr[0][0] = 11;
sparseArr[0][1] = 11;
sparseArr[0][2] = sum;
//4.遍历二维数组,将非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. 先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组
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();
}
}
}
输出结果:
边栏推荐
- Google Go to sea entrepreneurship accelerator registration countdown 3 days, entrepreneurs pass through the guide in advance collection!
- LeetCode 27. 移除元素
- VLAN experiment of switching technology
- Singleton mode compilation
- Talking about MySQL database
- 利用传统方法(N-gram,HMM等)、神经网络方法(CNN,LSTM等)和预训练方法(Bert等)的中文分词任务实现
- LeetCode 83. Delete duplicate elements in the sorting linked list
- ROS create workspace
- Bgp Routing preference Rules and notice Principles
- ZABBIX server trap command injection vulnerability (cve-2017-2824)
猜你喜欢
谷歌出海创业加速器报名倒计时 3 天,创业人闯关指南提前收藏!
Invalid operation: Load into table ‘sources_orderdata‘ failed. Check ‘stl_load_errors‘ system table
Current situation analysis of Devops and noops
WLAN相关知识点总结
ROS create workspace
Contest3147 - game 38 of 2021 Freshmen's personal training match_ 1: Maximum palindromes
来自读者们的 I/O 观后感|有奖征集获奖名单
经典文献阅读之--Deformable DETR
Deep learning classification network -- vggnet
The official zero foundation introduction jetpack compose Chinese course is coming!
随机推荐
Zabbix Server trapper 命令注入漏洞 (CVE-2017-2824)
JWT tool class
LeetCode 90. 子集 II
Contest3145 - the 37th game of 2021 freshman individual training match_ H: Eat fish
Redis Key-Value数据库 【秒杀】
Page printing plug-in print js
来自读者们的 I/O 观后感|有奖征集获奖名单
Monitoring uplink of VRRP
亚马逊aws数据湖工作之坑1
Unity shader learning notes (3) URP rendering pipeline shaded PBR shader template (ASE optimized version)
Stc8h8k Series Assembly and c51 Real combat - NIXIE TUBE displays ADC, Key Series port reply Key number and ADC value
LeetCode 27. 移除元素
深入了解JUC并发(二)并发理论
Leverage Google cloud infrastructure and landing area to build enterprise level cloud native excellent operation capability
Detailed steps of JS foreground parsing of complex JSON data "case: I"
AttributeError: ‘str‘ object has no attribute ‘decode‘
Contest3147 - game 38 of 2021 Freshmen's personal training match_ G: Flower bed
The real definition of open source software
Contest3147 - game 38 of 2021 Freshmen's personal training match_ 1: Maximum palindromes
Linkage between esp8266 and stc8h8k single chip microcomputer - Weather Clock