当前位置:网站首页>稀疏数组(非线性结构)
稀疏数组(非线性结构)
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();
}
}
}
输出结果:
边栏推荐
- 来自读者们的 I/O 观后感|有奖征集获奖名单
- BGP routing optimization rules and notification principles
- MUI底部导航的样式修改
- Stc8h8k series assembly and C51 actual combat - digital display ADC, key serial port reply key number and ADC value
- Web page user step-by-step operation guide plug-in driver js
- Reading classic literature -- Suma++
- Contest3147 - game 38 of 2021 Freshmen's personal training match_ E: Listen to songs and know music
- 如何使用MITMPROXy
- 深入学习JVM底层(四):类文件结构
- Stc8h8k series assembly and C51 actual combat - serial port sending menu interface to select different functions
猜你喜欢
Community theory | kotlin flow's principle and design philosophy
Detailed explanation of BGP message
Contest3147 - game 38 of 2021 Freshmen's personal training match_ 1: Maximum palindromes
Contest3145 - the 37th game of 2021 freshman individual training match_ H: Eat fish
【C语言】简单实现扫雷游戏
借力 Google Cloud 基础设施和着陆区,构建企业级云原生卓越运营能力
Deep learning classification network -- vggnet
Contest3147 - game 38 of 2021 Freshmen's personal training match_ F: Polyhedral dice
经典文献阅读之--SuMa++
Replace Django database with MySQL (attributeerror: 'STR' object has no attribute 'decode')
随机推荐
From design delivery to development, easy and efficient!
Redis key value database [seckill]
It is said that Kwai will pay for the Tiktok super fast version of the video? How can you miss this opportunity to collect wool?
Deep learning classification network -- Network in network
Lambda expressions and method references
穀歌出海創業加速器報名倒計時 3 天,創業人闖關指南提前收藏!
LeetCode 83. Delete duplicate elements in the sorting linked list
Contest3147 - game 38 of 2021 Freshmen's personal training match_ E: Listen to songs and know music
Google play academy team PK competition, official start!
Contest3147 - game 38 of 2021 Freshmen's personal training match_ G: Flower bed
Common means of modeling: combination
步骤详解 | 助您轻松提交 Google Play 数据安全表单
Spark overview
亚马逊aws数据湖工作之坑1
数据回放伴侣Rviz+plotjuggler
Mock simulate the background return data with mockjs
I/o impressions from readers | prize collection winners list
Invalid operation: Load into table ‘sources_orderdata‘ failed. Check ‘stl_load_errors‘ system table
TI毫米波雷达学习(一)
STC8H8K系列汇编和C51实战——串口发送菜单界面选择不同功能