We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 246d731 commit 2317d0bCopy full SHA for 2317d0b
problems/118-pascals-triangle.md
@@ -0,0 +1,44 @@
1
+## 题目
2
+
3
+* 118. 杨辉三角
4
5
+给定一个非负整数 numRows,生成「杨辉三角」的前 numRows 行。
6
7
+在「杨辉三角」中,每个数是它左上方和右上方的数的和。
8
9
+## 思路
10
11
+dp[i][j] = dp[i-1][j-1] + dp[i-1][j]
12
13
14
+## 代码
15
16
+```
17
+class Solution {
18
19
+ /**
20
+ * @param Integer $numRows
21
+ * @return Integer[][]
22
+ */
23
+ function generate($numRows) {
24
25
+ 1
26
+ 1 1
27
+ 1 2 1
28
29
+ dp[i][j] = dp[i-1][j-1] + dp[i-1][j]
30
31
+ $dp = [];
32
+ for ($i = 0; $i < $numRows; $i++) {
33
+ for ($j = 0; $j <= $i; $j++) {
34
+ if ($j == 0 || $j == $i) {
35
+ $dp[$i][$j] = 1;
36
+ } else {
37
+ $dp[$i][$j] = $dp[$i-1][$j-1] + $dp[$i-1][$j];
38
+ }
39
40
41
+ return $dp;
42
43
+}
44
0 commit comments