当前位置:网站首页>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();
}
}
}
边栏推荐
- Redis learning notes (continuously updating)
- Oracle paging query ~~rownum (line number)
- How to make datasets, train them into models and deploy them based on yolov5
- Smart Panel wifi Linkage Technology, esp32 wireless chip module, Internet of Things WiFi Communication Application
- QT compiling security video monitoring system 43- picture playback
- [SC] OpenService FAILED 5: Access is denied.
- 存储器的保护
- Mysql主从搭建与Django实现读写分离
- Gao Xiang slam14 notes on three Lie groups and Lie algebra
- QT compile 45 graphic report of security video monitoring system
猜你喜欢

无线物联网WiFi模块方案,ESP32-S3芯片技术,助力设备智能化

Construction case of Expressway Precast Beam Yard (with scheme text)

Smart panel WiFi linkage technology, esp32 wireless chip module, Internet of things WiFi communication application

InnoDB data storage structure – MySQL

How to make datasets, train them into models and deploy them based on yolov5
![[official testerhome] MTSC 2021 Shanghai and Shenzhen PPT download addresses](/img/a0/d1170b20d01a7a586d8ff68279f1d4.jpg)
[official testerhome] MTSC 2021 Shanghai and Shenzhen PPT download addresses

Let me tell you the benefits of code refactoring

QT compile 45 graphic report of security video monitoring system

MySQL master-slave construction and Django implementation of read-write separation

2022 examination questions and online simulation examination for safety management personnel of hazardous chemical business units
随机推荐
2022 electrician (elementary) operation certificate examination question bank and online simulation examination
疫情数据分析平台工作报告【4】跨域相关
Enterprise Architect v16
Create a new table in the database. There was no problem before. Today
关于线程池需要注意的几点
Gao Xiang slam14 notes on three Lie groups and Lie algebra
智能面板WiFi聯動技術,ESP32無線芯片模組,物聯網WiFi通信應用
How Windows installs multiple versions of MySQL and starts it at the same time
L1-064 AI core code valued at 100 million (20 points)
Daily practice (28): balance binary tree
Thousand word masterpiece "programming biography"
疫情数据分析平台工作报告【8.5】额外的爬虫和绘图
数据库新建表,以前没问题的,今天
EnterpriseTECH STAR Question
Question for the 3D printing lattice?
Gavin teacher's perception of transformer live class - rasa dialogue robot project practice in the field of education agency mode and core component source code analysis under the microservice of educ
[SC] OpenService FAILED 5: Access is denied.
Work report on epidemic data analysis platform [7] Alibaba cloud related
如何制作数据集并基于yolov5训练成模型并部署
Zabbix6.0新功能Geomap 地图标记 你会用吗?