Re-learn the data structure --- + classification sparse array

A classification data structure

1. The two types of data structures

Linear structure and nonlinear structure

1) linear structure

  • Linear structure is the most common data structure, characterized by the presence of a linear relationship between one element.
  • Linear structure was divided into two, one is sequentially stored (referred to as sequence table), another is a chain store (referred to as list). Continuous sequence of elements stored in the table. The storage elements of the list are not necessarily contiguous, element nodes stored in the data element and the address information of the neighboring elements.
  • Common linear structure: array, queues, linked lists, and stacks (here just talk about, the back of the specific content of the article expands elaborate).

2) nonlinear structure

Nonlinear structure is junction element may be multiple direct predecessor and more direct follow-up (think about the binary tree to understand, but not the only nonlinear structural binary tree).

  • Nonlinear structure comprising: a multi-dimensional arrays, the generalized table, the tree structure, the structure of FIG.

Second, the sparse array

1. sparse array (sparse array)

1) analyzes the scene

There is such a scenario, the number of steps required to achieve a record Go 10 * 10. The easiest you can use a two-dimensional array int [10] [10] can, but at the beginning of the board, the two-dimensional array is almost no meaningful data. If you can find this two-dimensional array of compression, recording only useful data methods just fine. This time the sparse array can come in handy.

2) sparse array

As described board, the beginning, most of the elements of the data record is 0, or when a value of the same array, the sparse array can be used to hold the array.

3) treatment of the sparse array is:

  • An array of records, a total of several odd row, how many different values
  • The ranks of the different elements and the value recorded in an array of small-scale, thereby compressing the size of the applet.

    4) For example:

    If there board 10 * 6 is as follows, expressed as a positive integer zi sequence, using the sparse array compression of the board there is a right side. Row 0, respectively: the number of rows, columns, and the total number of values. From the first row to the last, all the number of lines, number of columns, values.

Sparse array

Thus, originally 6 array 10 is compressed into three 7 saves memory space.

5) code implementation

Ideas analysis

(1) two-dimensional array to a sparse array
  1. Traversing the original two-dimensional array, the number of valid data obtained sum
  2. Create a sparse array sparseArr [sum + 1] [3]
  3. The valid data is filled by one of the sparse array sparseArr
  • Code:
/**
* 二维数组转稀疏数组
* 
* @param arr 原数组
* @return 稀疏数组
*/
public int[][] reserveSparseArray(int[][] arr) {
    // 统计有效数据
    int sum = 0;
    // 遍历稀疏数组
    for (int[] is : arr) {
        for (int num : is) {
            if (num != 0) {
                sum++;
            }
        }
    }
    // 创建稀疏数组
    int[][] sparseArr = new int[sum + 1][3];
    sparseArr[0][0] = arr.length;
    sparseArr[0][1] = arr[0].length;
    sparseArr[0][2] = sum;
    for (int i = 0; i < arr.length; i++) {
        for (int j = 0; j < arr[i].length; j++) {
            if (arr[i][j] != 0) {
                sparseArr[sum][0] = i;
                sparseArr[sum][1] = j;
                sparseArr[sum][2] = arr[i][j];
                sum--;
            }
        }
    }
    return sparseArr;
}
(2) turn the original array sparse array
  1. Reading a first row of the sparse array, removing the first row, the second number col, creating a two-dimensional array shessArr [row] [col]
  2. Traversing sparse array behind a few lines, fill in the effective value of the original array chessArr
  • Code:
/**
* 稀疏数组转二位数组
* 
* @param sparseArr 稀疏数组
* @return 原数组
*/
public static int[][] reserveOriginalArray(int[][] sparseArr) {
    // 根据稀疏数组第一行创建原数组
    int[][] originalArr = new int[sparseArr[0][0]][sparseArr[0][1]];
    // 把稀疏数组的值放回到原数组中
    for (int i = 1; i < sparseArr.length; i++) {
        int row = sparseArr[i][0];
        int col = sparseArr[i][1];
        int value = sparseArr[i][2];
        originalArr[row][col] = value;
    }
    return originalArr;
}

If a man unknown to concentrate fall asleep!
Like a friend you can leave your praise!

Guess you like

Origin www.cnblogs.com/huaiangg/p/12227639.html