LeetCode //C - 118. Pascal‘s Triangle

118. Pascal's Triangle

Given an integer numRows, return the first numRows of Pascal's triangle.

In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:

Example 1:

Input: numRows = 5
Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]

Example 2:

Input: numRows = 1
Output: [[1]]

Constraints:
  • 1 <= numRows <= 30

From: LeetCode

Link: 118. Pascal's Triangle


Solution:

Ideas:

1. Memory Allocation for the Triangle:

The function starts by allocating memory for an array of integer pointers, triangle, which will store the rows of Pascal's Triangle. The size of this array is numRows, each element of which will be a pointer to an array representing a row in the triangle.

2. Memory Allocation for Column Sizes:

It also allocates memory for an array returnColumnSizes that holds the size of each row. This is needed because each row of Pascal's Triangle has a different number of elements. For row i, there will be i + 1 elements.

3. Filling the Triangle:

The function then iterates over each row with two nested loops:

  • The outer loop runs from 0 to numRows - 1, setting up each row.
  • The inner loop runs through each row and calculates the value of each element based on the values from the previous row. The first and last elements of each row are always 1.

4. First and Last Elements:

Within each row, before entering the inner loop, the first element is set to 1. After the inner loop, the last element is also set to 1.

5. Summation Rule:

The key operation occurs inside the inner loop, where each element is the sum of the two elements directly above it from the previous row: triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j]. This is the summation rule that defines Pascal's Triangle.

6. Return Values:

Finally, the function sets the returnSize to the number of rows (numRows) to indicate how many rows are in the returned triangle and returns the pointer to the array of arrays, which is Pascal's Triangle.

Code:
c 复制代码
/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
int** generate(int numRows, int* returnSize, int** returnColumnSizes) {
    // Allocate memory for the array of arrays
    int** triangle = (int**)malloc(numRows * sizeof(int*));
    *returnColumnSizes = (int*)malloc(numRows * sizeof(int));

    for (int i = 0; i < numRows; i++) {
        // Set the return size for each row
        (*returnColumnSizes)[i] = i + 1;

        // Allocate memory for each row
        triangle[i] = (int*)malloc((*returnColumnSizes)[i] * sizeof(int));
        triangle[i][0] = 1; // first element is always 1

        // Set the elements of the triangle
        for (int j = 1; j < i; j++) {
            // Each element equals the sum of the elements above it
            triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j];
        }

        triangle[i][i] = 1; // last element is always 1
    }

    // Set the return size
    *returnSize = numRows;
    
    return triangle;
}
相关推荐
_饭团4 分钟前
字符串函数全解析:12 种核心函数的使用与底层模拟实现
c语言·开发语言·学习·考研·面试·蓝桥杯
Larry_Yanan5 分钟前
Qt网络开发之基于 QWebEngine 实现简易内嵌浏览器
linux·开发语言·网络·c++·笔记·qt·学习
想吃火锅10055 分钟前
【leetcode】105. 从前序与中序遍历序列构造二叉树
算法·leetcode·职场和发展
圣保罗的大教堂7 分钟前
leetcode 3567. 子矩阵的最小绝对差 中等
leetcode
呆瑜nuage34 分钟前
【复习系列】高频C/C++库函数手写实现指南与自定义类型的理解指南
c语言·c++·面试
AI+程序员在路上41 分钟前
CAN 总线与 Linux SocketCAN C 语言测试程序
linux·c语言·网络
Predestination王瀞潞44 分钟前
4.3.3 存储->微软文件系统标准(微软,自有技术标准):VFAT(Virtual File Allocation Table)虚拟文件分配表系统
linux·microsoft·vfat
HalvmånEver44 分钟前
Linux:socket套接字编程的基础概念
linux·运维·服务器
老鼠只爱大米1 小时前
LeetCode经典算法面试题 #215:数组中的第K个最大元素(快速选择、堆排序、计数排序等多种实现方案详解)
算法·leetcode·堆排序·快速选择·topk·数组中的第k个最大元素
逆境不可逃1 小时前
LeetCode 热题 100 之 35. 搜索插入位置 74. 搜索二维矩阵 34. 在排序数组中查找元素的第一个和最后一个位置
数据结构·算法·leetcode