给出一个值numRows,生成杨辉三角的前numRows行
例如,给出 numRows = 5,返回
[↵ [1],↵ [1,1],↵ [1,2,1],↵ [1,3,3,1],↵ [1,4,6,4,1]↵]
Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,Return
[↵ [1],↵ [1,1],↵ [1,2,1],↵ [1,3,3,1],↵ [1,4,6,4,1]↵]
<?php
function getRow($row) {
$arrRow = [
0 => [1],
1 => [1, 1],
];
if ($row == 1) {
return $arrRow[0];
}
if ($row == 1) {
return $arrRow;
}
for ($i = 2; $i < $row; $i ++) {
$arrRow[$i][0] = 1;
for ($j = 1; $j < $i; $j ++) {
$arrRow[$i][$j] = $arrRow[$i - 1][$j - 1] + $arrRow[$i - 1][$j];
}
$arrRow[$i][$i] = 1;
}
return $arrRow;
}
$k = 5;
$ret = getRow($k);
print json_encode($ret);