当前位置:网站首页>Sparse array (sparse)
Sparse array (sparse)
2022-07-22 17:27:00 【Camellia——】
1. Basic introduction
When most of the elements in an array are 0, Or an array of the same value , You can save the array using a sparse array
2. Sparse array processing method
1) There are several rows and columns in the record array
2) Record the row and column values of elements with different values in a small array , To reduce the size of the program
3. Two dimensional array to sparse array
1) Traversing the original two-dimensional array , Number of valid data obtained sum
2) according to sum You can create a sparse array sparseArr int [sum+1][3]
3) Store the valid data of a two-dimensional array into a sparse array
package com.lin.sparsearray;
/*
* Checkerboard storage : Sunspots are in the second row and third column of the chessboard , Baizi is in the third row and the fourth column
* Two dimensional array to sparse array
* 1) Traversing the original two-dimensional array , Number of valid data obtained sum
2) according to sum You can create a sparse array sparseArr int [sum+1][3]
3) Store the valid data of a two-dimensional array into a sparse array
*
*
* */
public class SparseArray {
public static void main(String[] args) {
// Create an original array 11*11
// 0: No chess pieces ,1 Said the spots ,2 Said an albino
int chessArr1[][]=new int[11][11];
// The position of sunspots in the original array
chessArr1[1][2]=1;
// The position of the white child in the original array
chessArr1[2][3]=2;
System.out.println(" The original two-dimensional array :");
// Output the original two-dimensional array
for(int arr[]:chessArr1) {
for(int data:arr) {
System.out.printf("%d\t",data);
}
System.out.println();
}
// Convert a two-dimensional array to a sparse array
//1. First, traverse the two-dimensional array Get the blame 0 Number of data
int sum=0;// The record is not 0 Number of data
for(int i=0;i<chessArr1.length;i++) {
for(int j=0;j<chessArr1.length;j++) {
if(chessArr1[i][j]!=0) {
sum++;
}
}
}
System.out.println("sum="+sum);
//2. Create the corresponding sparse array
int sparseArr[][]=new int[sum+1][3];
// Assign values to sparse arrays
sparseArr[0][0]=11;
sparseArr[0][1]=11;
sparseArr[0][2]=sum;
int count=0;// It's the number one non 0 data
for(int i=0;i<chessArr1.length;i++) {
for(int j=0;j<chessArr1.length;j++) {
if(chessArr1[i][j]!=0) {
count++;
sparseArr[count][0]=i;
sparseArr[count][1]=j;
sparseArr[count][2]=chessArr1[i][j];
}
}
}
// Output sparse array form
System.out.println();
System.out.println(" The sparse array is :");
for(int i=0;i<sparseArr.length;i++) {
for(int j=0;j<3;j++) {
System.out.print(sparseArr[i][j]+" ");
}
System.out.println();
}
}
}
4. The idea of transforming sparse array into original two-dimensional array
1) First read the first row of the sparse array , According to the data in the first line , Create the original two-dimensional array
2) After reading the sparse array a few lines of data , And assign it to the original two-dimensional array
package com.lin.sparsearray;
/*
* Checkerboard storage : Sunspots are in the second row and third column of the chessboard , Baizi is in the third row and the fourth column
* Two dimensional array to sparse array
* 1) Traversing the original two-dimensional array , Number of valid data obtained sum
2) according to sum You can create a sparse array sparseArr int [sum+1][3]
3) Store the valid data of a two-dimensional array into a sparse array
*
*
* */
public class SparseArray {
public static void main(String[] args) {
// Create an original array 11*11
// 0: No chess pieces ,1 Said the spots ,2 Said an albino
int chessArr1[][]=new int[11][11];
// The position of sunspots in the original array
chessArr1[1][2]=1;
// The position of the white child in the original array
chessArr1[2][3]=2;
System.out.println(" The original two-dimensional array :");
// Output the original two-dimensional array
for(int arr[]:chessArr1) {
for(int data:arr) {
System.out.printf("%d\t",data);
}
System.out.println();
}
// Convert a two-dimensional array to a sparse array
//1. First, traverse the two-dimensional array Get the blame 0 Number of data
int sum=0;// The record is not 0 Number of data
for(int i=0;i<chessArr1.length;i++) {
for(int j=0;j<chessArr1.length;j++) {
if(chessArr1[i][j]!=0) {
sum++;
}
}
}
System.out.println("sum="+sum);
//2. Create the corresponding sparse array
int sparseArr[][]=new int[sum+1][3];
// Assign values to sparse arrays
sparseArr[0][0]=11;
sparseArr[0][1]=11;
sparseArr[0][2]=sum;
int count=0;// It's the number one non 0 data
for(int i=0;i<chessArr1.length;i++) {
for(int j=0;j<chessArr1.length;j++) {
if(chessArr1[i][j]!=0) {
count++;
sparseArr[count][0]=i;
sparseArr[count][1]=j;
sparseArr[count][2]=chessArr1[i][j];
}
}
}
// Output sparse array form
System.out.println();
System.out.println(" The sparse array is :");
for(int i=0;i<sparseArr.length;i++) {
for(int j=0;j<3;j++) {
System.out.print(sparseArr[i][j]+" ");
}
System.out.println();
}
/*
* Convert the sparse array to the original array
* 1) First read the first row of the sparse array , According to the data in the first line , Create the original two-dimensional array
2) After reading the sparse array a few lines of data , And assign it to the original two-dimensional array
*
* */
// 1) First read the first row of the sparse array , According to the data in the first line , Create the original two-dimensional array
int chessArr2[][]=new int[sparseArr[0][0]][sparseArr[0][1]];
// 2) After reading the sparse array a few lines of data , And assign it to the original two-dimensional array
System.out.println();
System.out.println(" Recovered 2D array :");
for(int i=1;i<sparseArr.length;i++) {
chessArr2[sparseArr[i][0]][sparseArr[i][1]]=sparseArr[i][2];
}
for(int arr[]:chessArr2) {
for(int data:arr) {
System.out.printf("%d\t",data);
}
System.out.println();
}
}
}
边栏推荐
猜你喜欢
How to implement Apache's built-in AB stress testing tool
Principle and performance analysis of lepton lossless compression
【图文并茂】在线一键重装win7系统详细教程
【LeetCode】814. 二叉树剪枝
How to reinstall win11 system online with one click?
How to download and install xshell plus6
报表设计工具FastReport Online Designer V2022.1新改变全介绍
unity PostProcess 屏幕后处理
【外部排序】归并思想完成外部排序
浏览器页面的渲染流程
随机推荐
Information security CISP certification - what are your concerns?
【C语言趣味实验】
How to use js to realize calculator and timer functions
mysql约束之默认约束default
Solidworks基础特征你了解多少?| 拉伸特征的4种方法
【外部排序】快排思想完成外部排序
JS的DOM操作——事件链(冒泡目标捕获)
Activity recommendation | Apache pulsar's exploration and practice in vivo will be broadcast soon
The three formats of the log "binlog" in MySQL are so interesting
Atomicinteger class is used in multithreading to ensure thread safety
【转载】UE4 面试基础知识(二)
Matlab function: filtfilter -- zero phase digital filtering
What is I18N and what is its function
Server network performance tuning tool
This article introduces you to the workflow of Redux - action/reducer/store
Pytorch deep learning practice-b station Liu erden-day2
【外部排序】归并思想完成外部排序
微信营销策略,微信营销的优势是什么?
NPM warn config global ` --global `, `--local` are deprecated Use `--location=global` instead.
《PyTorch深度学习实践》-B站 刘二大人-day1