当前位置:网站首页>Day18 creation and restoration of sparse array
Day18 creation and restoration of sparse array
2022-06-12 04:38:00 【33 year old Java enthusiast】
Sparse array
Record the coordinates and values of the valid values
When most elements of an array are 0, Or an array of the same value , You can use a sparse array to hold the array .
Sparse array processing :
- There are several rows and columns in the record array , How many different values
- Record elements, rows, columns and values with different values in a small array , So as to reduce the scale of the program .
Pictured
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-p8BKZLLM-1646094194934)(1645884023838.png)]
Operation steps
How to compress into a sparse array
- First count the number of elements in the array
- Create a sparse array again That's ok Column value , Remember that the number of rows is a statistical value +1( Look up ), Remember to assign the total number of header rows , Sum of columns
- Traversal again does not mean 0 Value , Get the number of rows and the number of columns and values
How to decompress a sparse array
- The first value of the first row of the sparse array is the row of the original array , The second value in the first row is the column of the original array , The third value in the first row is the value of the original array
package com.ckw.blog.arrays;
public class arrayxishu02 {
public static void main(String[] args) {
int[][] array =new int[11][11];
array[1][3] = 8;
array[3][7] = 21;
array[5][2] = 12;
array[6][3] = 45;
array[10][9] = 67;
// Created an array
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j]+"\t");
}
System.out.println();
}
System.out.println("=============================");
//1. Get the sum of the values in the array
int count = 0;// Counter
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
if(array[i][j]!=0){
count++;
}
}
}
//2. Create a two-dimensional sparse array . That's ok Column value . Remember that the number of rows is a statistical value +1( Look up ), Remember to assign the total number of header rows , Sum of columns
int[][] array02 = new int[count+1][3]; // Remember to add 1 As header
array02[0][0] =array.length;
array02[0][1] =array[0].length;
array02[0][2] =count;
int nub = 0;
//3. Traversal again does not mean 0 Value , Get the number of rows and the number of columns and values
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
if(array[i][j]!=0){
nub++;
array02[nub][0]=i;
array02[nub][1]=j;
array02[nub][2]=array[i][j];
}
}
}
for(int[] ax:array02){
for (int xa:ax){
System.out.print(xa+"\t");
}System.out.println();
}
System.out.println("=============================");
// Restore The first value of the first row of the sparse array is the row of the original array , The second value in the first row is the column of the original array , The third value in the first row is the value of the original array
int[][] array03 = new int[array02[0][0]][array02[0][1]];
for (int i = 1; i < array02.length; i++) {
for (int j = 0; j < array02[j].length; j++) {
array03[array02[i][0]][array02[i][1]]=array02[i][2];
}
}
System.out.println("========================================");
for (int i = 0; i < array03.length; i++) {
for (int j = 0; j < array03.length; j++) {
System.out.print(array03[i][j]+"\t");
}
System.out.println();
}
}
}
边栏推荐
- Work report of epidemic data analysis platform [6] visual drawing
- mysqld: Can‘t create directory ‘D: oftinstall\mysql57 (Errcode: 2 - No such file or directory)
- Summary of common interview questions in redis
- EN in Spacey_ core_ web_ SM installation problems
- QT compile 45 graphic report of security video monitoring system
- 树莓派4B使用Intel Movidius NCS 2来进行推断加速
- 加速训练之并行化 tf.data.Dataset 生成器
- JS function and variable have the same name (function and variable parsing rules)
- leetcode797. 所有可能的路径(中等)
- Create a new table in the database. There was no problem before. Today
猜你喜欢

图解 Apache SkyWalking UI 的使用

2022 low voltage electrician test questions and simulation test

Tasks in C #

D1 哪吒开发板 上电记录

kali_ Nat mode, bridging Internet / host only_ detailed

How do I extract files from the software?

AI and logistics Patent

【高效】最强开发工具Ctool编译踩坑
![[wechat applet] the mobile terminal selects and publishes pictures](/img/9a/46bc4a7bf9b70d26b0e24fe02f747d.jpg)
[wechat applet] the mobile terminal selects and publishes pictures

Install pycharm under Kali and create a shortcut access
随机推荐
疫情数据分析平台工作报告【8.5】额外的爬虫和绘图
Gao Xiang slam14 notes on three Lie groups and Lie algebra
如何制作数据集并基于yolov5训练成模型并部署
JWT学习与使用
Work report of epidemic data analysis platform [1] data collection
Work report of epidemic data analysis platform [4] cross domain correlation
Why should a redis cluster use a reverse proxy? Just read this one
C# Task. Waitall method
Work report of epidemic data analysis platform [6] visual drawing
one billion one hundred and eleven million one hundred and eleven thousand one hundred and eleven
Oracle's instr()
树莓派4B使用Intel Movidius NCS 2来进行推断加速
Kill session? This cross domain authentication solution is really elegant!
Enterprise Architect v16
Simple Tetris
Is there a row limit for a single MySQL table
How Windows installs multiple versions of MySQL and starts it at the same time
What are the black box test case design methods in software testing methods?
Manually encapsulate a foreacht and map
eBPF系列学习(4)了解libbpf、CO-RE (Compile Once – Run Everywhe) | 使用go开发ebpf程序(云原生利器cilium ebpf )