-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path118.杨辉三角.php
62 lines (58 loc) · 1.12 KB
/
118.杨辉三角.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
/*
* @lc app=leetcode.cn id=118 lang=php
*
* [118] 杨辉三角
*
* https://leetcode-cn.com/problems/pascals-triangle/description/
*
* algorithms
* Easy (70.74%)
* Likes: 493
* Dislikes: 0
* Total Accepted: 171.4K
* Total Submissions: 241.4K
* Testcase Example: '5'
*
* 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。
*
*
*
* 在杨辉三角中,每个数是它左上方和右上方的数的和。
*
* 示例:
*
* 输入: 5
* 输出:
* [
* [1],
* [1,1],
* [1,2,1],
* [1,3,3,1],
* [1,4,6,4,1]
* ]
*
*/
// @lc code=start
class Solution
{
/**
* @param int $numRows
* @return int[][]
*/
public function generate($numRows)
{
$a = [] ;
for ($i = 0; $i <= $numRows; $i++) {
for ($j = 0; $j < $i; $j ++) {
if (isset($a[$i - 1][$j - 1]) && $a[$i - 1][$j]) {
$a[$i][$j] = $a[$i - 1][$j - 1] + $a[$i - 1][$j];
} else {
$a[$i][$j] = 1;
}
}
}
return $a;
}
}
// @lc code=end