Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2317d0b

Browse files
committed
Feat: 118
1 parent 246d731 commit 2317d0b

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

‎problems/118-pascals-triangle.md‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /