On July 4, 2022, leetcode checks in one question per day——118. Yang Hui Triangle

1. Topic description and requirements

118. Yang Hui Triangle - LeetCode

topic description

Given a non-negative integer  numRows, generate antecedents of the Yanghui Triangle  numRows .

In the Yang Hui Triangle, each number is the sum of the numbers above it to the left and above it to the right.

example

Example 1:

输入: numRows = 5
输出: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]

Example 2:

输入: numRows = 1
输出: [[1]]

hint

  • 1 <= numRows <= 30

2. Problem-solving ideas

General idea:

First of all, when you see Yang Hui's triangle, the first thing you should think of is a two-dimensional array, because each number in Yang Hui's triangle is the sum of the numbers on its upper left and upper right. But it should be noted that if it is a two-dimensional array, it should be a square, but part of the space does not store values, which requires us to use the idea of ​​​​dynamic programming. First apply for a secondary pointer ret of the pointer array. In fact, this ret is equivalent to the name of the two-dimensional array, except that it points to the pointer array of size numRows, which is the array pointing to the first element of each row. Then, assign numRows to returnSize and returnColumnSizes according to the hints in the comments. The purpose is to return numRows arrays when returning at the end, and store i+1 elements in each array. The next step is to assign values ​​to this two-dimensional array. First, apply for i+1 storage spaces for each row to store numbers. returnColumnSizes is a secondary pointer, which points to an integer array with a length of numRows, so (returnColumnSizes)[i] refers to the number of storage elements in each row, which should be equal to i+1, and then the first row of each row and the last one are assigned a value of 1, and then the value of the upper left and upper right of each element is added to it. In this way, a Yang Hui triangle is established.

Note that returnSize is an integer pointer, which points to an integer, and is only used to record the number of rows returned, so returnSize=numRows; and returnColumnSizes is a secondary pointer, pointing to an integer array of length numRows, so (*returnColumnSizes )[i] is to indicate the number of elements in each row, which has nothing to do with the establishment of the Yang Hui triangle, but is related to the final return result.

Only by determining the size of the returned array will it not lead to waste of space.

Specific steps:

1. Apply for the storage space for Yang Hui’s triangle
2. Assign the number of returned arrays and the number of elements in the array
3. Assign the value of Yang Hui’s triangle
4. Assign the first and last numbers of each row to 1, and the remaining The one below is the sum of the upper left and upper right numbers, the upper left is [i-1][j-1], and the upper right is [i-1][j].
5. Return the final result according to the size

3. Specific code

/**
 * Return an array of arrays of size *returnSize.//返回大小为*returnSize的数组
 * The sizes of the arrays are returned as *returnColumnSizes array.//数组的大小作为*returnColumnSizes数组返回
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
int** generate(int numRows, int* returnSize, int** returnColumnSizes){
    //ret相当于一个二维数组,ret[i]指向的是第i行
    int** ret=malloc(sizeof(int*)*numRows);//ret为指向大小为numRows个整形指针数组的指针
    *returnSize=numRows;
    *returnColumnSizes=malloc(sizeof(int)*numRows);//指向大小为numRows的整型数组
    for(int i=0;i<numRows;i++)//二维数组赋值
    {
        ret[i]=malloc(sizeof(int)*(i+1));//申请空间,每一行的个数应当是i+1,因此所形成的是三角形而不是正方形
        (*returnColumnSizes)[i]=i+1;//返回的数组中的每一行的元素个数
        ret[i][0]=ret[i][i]=1;//每一行的第一个元素还有最后一个元素都是1
        for(int j=1;j<i;j++)
        {
            ret[i][j]=ret[i-1][j]+ret[i-1][j-1];//每个数等于它左上方也就是[i-1][j-1]的和右上方[i-1][j]的和
        }
    }
    return ret;
}

Guess you like

Origin blog.csdn.net/m0_59800431/article/details/125603408