# Leetcode 118. Pascal's Triangle ###### tags: `Leetcode` 題目 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]] 解法: 1.用一個for圈代表rows,再根據每個row再用一個for迴圈,分別頭尾放1=>res[i][j] = 1;,中間的值就用計算的=>res[i][j] = res[i-1][j-1] + res[i-1][j]; 註: 1. *(res+i) equal to res[i] 2. *(res)[i] equal to res[0][i] ``` /** * 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){ if(numRows == 0) return 0; int** res = malloc(sizeof(int*)*numRows); *returnSize = numRows; *returnColumnSizes = malloc(sizeof(int)*numRows); for(int i = 0; i < numRows; i++) { *((*returnColumnSizes)+i) = i+1; *(res+i) = (int*)malloc(sizeof(int)*(i+1)); for(int j = 0; j <= i; j++) { if(j == 0 || j == i) res[i][j] = 1; else res[i][j] = res[i-1][j-1] + res[i-1][j]; } } return res; } ```