当前位置:网站首页>Sparse array [matrix]
Sparse array [matrix]
2022-07-05 22:14:00 【Cc824 constant】
Sparse array
- first line : Record the original array, which has several rows and columns , Several different values
- Residual downlink : Record the rows, columns and values of elements with different values in an array , To reduce the size of the program
Two dimensional array turn The idea of sparse array
1. Traverse Original two-dimensional array , Get the number of valid data sum
2. according to sum establish Sparse array sparseArr int[sum+1][3] 3 It must be the same
3. Store the valid data of the two-dimensional array into the sparse array
Sparse array to original two-dimensional array idea
1. First read the first two data in the first row of the sparse array Create the original two-dimensional array
2. Read the data of the last few rows of the sparse array And assign it to the original array
public class T1 {
public static void main(String[] args) {
// Compress a two-dimensional array into a sparse array
//1. Create a 2D array
int chessArr[][] = new int[11][11];
chessArr[1][2] = 1;
chessArr[2][3] = 2;
int sum = 0;
//2. Get the number of valid data
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++)
if (chessArr[i][j] != 0)
sum++;
}
//3. Create a sparse array
int spareArr[][] = new int[sum+1][3];
spareArr[0][0] = 11;
spareArr[0][1] = 11;
spareArr[0][2] = sum;
//4. assignment
int count = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (chessArr[i][j] != 0){
count++;
spareArr[count][0] = i ;
spareArr[count][1] = j ;
spareArr[count][2] = chessArr[i][j] ;
}
}
}
System.out.println(" Output sparse array ");
for (int i=0;i<spareArr.length;i++)
System.out.printf("%d\t%d\t%d\t\n",spareArr[i][0],spareArr[i][1],spareArr[i][2]);
System.out.println("==============");
// Sparse array to two-dimensional array
//1. Read the first two data of the first row of the sparse array , Then create a new two-dimensional array
int chessArr1[][] = new int[spareArr[0][0]][spareArr[0][1]];
//2. Traverse the second row of the sparse array, followed by , Then assign a value to a two-dimensional array
for (int i = 1;i< spareArr.length;i++){
chessArr1[spareArr[i][0]][spareArr[i][1]] = spareArr[i][2];
}
//3. Output two-dimensional array
for (int[] row : chessArr1){
for (int data :row){
System.out.printf("%d\t",data);
}
System.out.println();
}
}
}
Save sparse arrays to disk , And restore 【 New function 】
Save sparse array
// Read to disk
// Get file object
File file = new File("E:\\map.data");
// File write stream
FileWriter writer = new FileWriter(file);
// Traversal array , write file
for (int[] sparse:sparseArr) {
for (int s:sparse) {
writer.write(s+"\t");
}
writer.write("\n");
}
writer.close();
Restore sparse array
// Read data from disk
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = "";
int[][] chessArr2 = null;
int row = 0;
// Loop through every line of the file
while ((line=reader.readLine())!=null){
// Press tab Bits are cut into string arrays , And then to int Array
String[] temp = line.split("\t");
int[] ints = new int[temp.length];
for (int i = 0; i < ints.length; i++) {
ints[i] = Integer.parseInt(temp[i]);
}
row++;
// The first line of the document , That is, the first row of the sparse array , Is the length and width of the original array
if (row==1){
chessArr2 = new int[ints[0]][ints[1]];
}else{
// Add the rest , Where there is no assignment , The array will be initialized to 0
chessArr2[ints[0]][ints[1]] = ints[2];
}
}
reader.close();
}
边栏推荐
- Matlab draws a cute fat doll
- 数据泄露怎么办?'华生·K'7招消灭安全威胁
- 如何开发引入小程序插件
- Alternating merging strings of leetcode simple questions
- [Yugong series] go teaching course in July 2022 004 go code Notes
- Getting started with microservices (resttemplate, Eureka, Nacos, feign, gateway)
- 了解 Android Kotlin 中 DataStore 的基本概念以及为什么应该停止在 Android 中使用 SharedPreferences
- 笔记本电脑蓝牙怎么用来连接耳机
- Two stage locking protocol for concurrency control
- Kubernetes Administrator certification (CKA) exam notes (IV)
猜你喜欢
MySQL disconnection reports an error MySQL ldb_ exceptions. OperationalError 4031, The client was disconnected by the server
K210 learning notes (IV) k210 runs multiple models at the same time
Concurrency control of performance tuning methodology
MySQL连接断开报错MySQLdb._exceptions.OperationalError 4031, The client was disconnected by the server
[Yugong series] go teaching course 003-ide installation and basic use in July 2022
K210学习笔记(四) K210同时运行多个模型
How to view Apache log4j 2 remote code execution vulnerability?
ICMP 介绍
Huawei cloud modelarts text classification - takeout comments
A substring with a length of three and different characters in the leetcode simple question
随机推荐
A trip to Suzhou during the Dragon Boat Festival holiday
Business learning of mall order module
IIC bus realizes client device
How to use tensorflow2 for cat and dog classification and recognition
Cross end solutions to improve development efficiency
How to view Apache log4j 2 remote code execution vulnerability?
The new content of the text component can be added through the tag_ Config set foreground and background colors
Search: Future Vision (moving sword)
Calculation method of boundary IOU
Interprocess communication in the "Chris Richardson microservice series" microservice architecture
Granularity of blocking of concurrency control
Understand the basic concept of datastore in Android kotlin and why SharedPreferences should be stopped in Android
Matlab draws a cute fat doll
Official clarification statement of Jihu company
What changes has Web3 brought to the Internet?
K210学习笔记(四) K210同时运行多个模型
The difference between MVVM and MVC
The Blue Bridge Cup web application development simulation competition is open for the first time! Contestants fast forward!
2022-07-05:给定一个数组,想随时查询任何范围上的最大值。 如果只是根据初始数组建立、并且以后没有修改, 那么RMQ方法比线段树方法好实现,时间复杂度O(N*logN),额外空间复杂度O(N*
When the industrial Internet era is truly mature, we will look at the emergence of a series of new industrial giants