当前位置:网站首页>稀疏数组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();
}
}
}
边栏推荐
- 如何设计好的技术方案
- MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications
- Thread status and stop
- 302. minimum rectangular BFS with all black pixels
- Spark source code analysis (I): RDD collection data - partition data allocation
- Hot! 11 popular open source Devops tools in 2021!
- numpy.exp()
- Matching environment of ES6
- Dpdk - tcp/udp protocol stack server implementation (II)
- How to associate wechat applet QR code to realize two code aggregation
猜你喜欢

Understanding of nil in go language
Web components series (10) -- realize the basic layout of mycard

On site commissioning - final method of kb4474419 for win7 x64 installation and vs2017 flash back

Typora activation method

GoF23—抽象工厂模式

C generic speed

架构设计方法

How to design a good technical scheme

Transaction and message semantics

Tencent's 2022 school recruitment of large factories started with salary, and the general contracting of cabbage is close to 40W!
随机推荐
Comparison between Prometheus and ZABBIX
Func < T, tresult > Commission - learning record
去哪儿网BI平台建设演进史
A tragedy triggered by "yyyy MM DD" and vigilance before New Year's Day~
Getting started with Python
GoF23—原型模式
Tencent's 2022 school recruitment of large factories started with salary, and the general contracting of cabbage is close to 40W!
Easy to understand from the IDE, and then talk about the applet IDE
Logstash——使用throttle过滤器向钉钉发送预警消息
Vs2022 offline installation package download and activation
Deeply uncover Ali (ant financial) technical interview process with preliminary preparation and learning direction
MySQL-05
SSH keygen specifies the path
The difference between abstract and interface interface
Library management system
Thinking skills of technical leaders
ByteDance starts the employee's sudden wealth plan and buys back options with a large amount of money. Some people can earn up to 175%
Class and object learning
Several promotion routines of data governance
PyTorch使用多GPU并行训练及其原理和注意事项