• 稀疏数组


    稀疏 sparsearray 数组

    简介

    矩阵中非零元素的个数远远小于矩阵元素的总数,并且非零元素的分布没有规律,通常认为矩阵中非零元素的总数比上矩阵所有元素总数的值小于等于0.05时,则称该矩阵为稀疏矩阵(sparse matrix),该比值称为这个矩阵的稠密度;

    当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组。

    实现思路

    二维数组 转 稀疏数组的思路

    1. 遍历 原始的二维数组,得到有效数据的个数 sum
    2. 根据sum 就可以创建 稀疏数组 sparseArr int[有效个数+1] [3]
    3. 将二维数组的有效数据数据存入到 稀疏数组

    稀疏数组转原始的二维数组的思路

    1. 先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组

    2. 在读取稀疏数组后几行的数据,并赋给 原始的二维数组 即可.

    代码实现

         /**
             * 初始化一个原型数组
             */
            int original[][] = new int[5][9];
            original[0][1] = 1;
            original[1][2] = 2;
    
    
            System.out.println("稀疏数组原型");
            for (int[] ints : original) {
                for (int anInt : ints) {
                    System.out.print(anInt + "	");
                }
                System.out.println();
            }
            int count = 0;//非空数
            int row = original.length;//行数
            int col = original[0].length;//列数
            for (int[] ints : original) {
    
                for (int anInt : ints) {
    
                    if (anInt != 0) {
                        count++;
                    }
                }
            }
            System.out.println("数量:" + count);
            System.out.println("行数:" + row);
            System.out.println("列数" + col);
            /**
             * 稀疏数组第一行 存贮 原数组存在值的 行数 列数 值
             */
            int now[][] = new int[count + 1][3];
            now[0][0] = row;
            now[0][1] = col;
            now[0][2] = count;
    
            /**
             * 将原型数组装换为稀疏数组
             */
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < col; j++) {
                    if (original[i][j] != 0) {
                        now[i + 1][0] = i;
                        now[i + 1][1] = j;
                        now[i + 1][2] = original[i][j];
                    }
    
                }
            }
    
    
            /**
             * 打印稀疏数组
             */
            for (int[] ints : now) {
                for (int anInt : ints) {
                    System.out.print(anInt + "	");
                }
                System.out.println();
            }
    
            /**
             * 读取稀疏数组转换为原型数组
             */
            int reduction[][] = new int[now[0][0]][now[0][1]];
            for (int i = 1; i < now[0][2] + 1; i++) {
                reduction[now[i][0]][now[i][1]] = now[i][2];
            }
    
    
            System.out.println("稀疏数组还原型");
            for (int[] ints : original) {
                for (int anInt : ints) {
                    System.out.print(anInt + "	");
                }
                System.out.println();
            }
    
  • 相关阅读:
    Hello TensorFlow 二 (GPU)
    Hello TensorFlow
    C/C++调用Golang 二
    C/C++调用Golang 一
    再谈cgo(转)
    Go语言第一深坑
    通过样式改变图片明暗度,不是透明度哦
    webpack vue-loader was used without the corresponding plugin. Make sure to include VueLoaderPlugin
    webpack打包报错Chunk.entrypoints: Use Chunks.groupsIterable and filter by instanceof Entrypoint instead
    [Vue warn]: You are using the runtime-only build of Vue where the template compiler is not available. Either pre-compile the templates into render functions, or use the compiler-included build.
  • 原文地址:https://www.cnblogs.com/huangshen/p/13210763.html
Copyright © 2020-2023  润新知