当前位置:网站首页>稀疏数组sparsearray
稀疏数组sparsearray
2022-06-26 06:12:00 【Oh No 发量又少了】
1.基本介绍
当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组。
稀疏数组的处理方法是:
- 记录数组一共有几行几列,有多少个不同的值
- 把具有不值的元素的行列及值记录在一个小规模的数组中,从而缩小程序的规模
2.转换思路
二维数组转稀疏数组的思路
- 遍历原始的二维数组,得到有效数的个数sum
- 根据sum就可以创建稀疏数组sparseArr int[sum+1][3]
- 将二维数组的有效数据存入到稀疏数组
稀疏数组转原始二维数组的思路 - 先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组,比如上面的chessArr2 = int [11][11]
- 在读取稀疏数组后几行的数据,并赋给原始的二维数组即可。
3. 代码实现
原始二位数组转稀疏数组
- 先创建原始二维数组
- 原始二维数组转稀疏数组
a. 先遍历二维数组,得到非零数据的个数sum
b. 创建稀疏数组
c. 给稀疏数组赋值
d. 将二维数组中非零数据存入二维数组
package the10sessionA.test1.chessArray;
//稀疏数组与普通二维数组之间的转换
public class Main {
public static void main(String[] args) {
//1.先创建一个普同的二维数组
int array1 [][] = new int[11][11];
array1 [5][6] = 1;
array1 [6][5] = 2;
//2.便利普通二维数组记录非零数据的个数sum
int sum = 0;
int x = array1.length;
int y = 0;
for (int [] data : array1) {
for (int i : data) {
if (i != 0) {
sum++;
}
}
y = data.length;
}
System.out.println("得sum="+sum);
int chessArray [][] = new int[sum+1][3];
chessArray [0][0] = x;
chessArray [0][1] = y;
chessArray [0][2] = sum;
int temp = 1;
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
if (array1[i][j] != 0) {
chessArray[temp][0] = i;
chessArray[temp][1] = j;
chessArray[temp][2] = array1[i][j];
temp++;
}
}
}
System.out.println("输出chessArray数组");
for (int [] data : chessArray) {
for (int i : data) {
System.out.print(i+" ");
}
System.out.println();
}
//稀疏数组转二维数组
int array2 [][] = new int[chessArray [0][0]][chessArray [0][1]];
for (int i = 1; i < chessArray.length; i++) {
array2[chessArray[i][0]][chessArray[i][1]] = chessArray[i][2];
}
//输出有稀疏数组转换而来的二维数组
for (int [] data : array2) {
for (int i : data) {
System.out.print(i+" ");
}
System.out.println();
}
}
}
边栏推荐
- Tencent WXG internship experience (has offered), I hope it will help you!
- Class and object learning
- GoF23—抽象工厂模式
- 【群内问题学期汇总】初学者的部分参考问题
- Comparison between Prometheus and ZABBIX
- GoF23—建造者模式
- Print bit information of numbers
- Five solutions across domains
- Transformer中的Self-Attention以及Multi-Head Self-Attention(MSA)
- canal部署、原理和使用介绍
猜你喜欢

Message queue - function, performance, operation and maintenance comparison

Efk Upgrade to clickhouse log Storage Reality

EFK昇級到ClickHouse的日志存儲實戰

PyTorch使用多GPU并行训练及其原理和注意事项
Web components series (10) -- realize the basic layout of mycard

在web页面播放rtsp流视频(webrtc)

Introduction to canal deployment, principle and use

A tragedy triggered by "yyyy MM DD" and vigilance before New Year's Day~

EFK升级到ClickHouse的日志存储实战

Household accounting procedures (First Edition)
随机推荐
Yamaha robot splits visual strings
numpy.exp()
[intra group questions semester summary] some reference questions for beginners
Design and practice of low code real-time data warehouse construction system
Logstash——使用throttle过滤器向钉钉发送预警消息
Redis underlying data structure
Playing RTSP streaming video on Web pages (webrtc)
Class and object learning
numpy.random.choice
numpy. random. choice
MVC source code sharing
numpy. log
Message queue - function, performance, operation and maintenance comparison
Import export simple
C generic speed
TCP连接与断开,状态迁移图详解
MySQL-07
tf. nn. top_ k()
Mysql-10 (key)
Handwritten background management framework template (I)