给出一个索引k,返回杨辉三角的第k行
例如,k=3, 返回[1,3,3,1].
备注:你能将你的算法优化到只使用O(k)的额外空间吗?
Given an index k, return the k th row of the Pascal's triangle.
For example, given k = 3,
Return[1,3,3,1].
Note: Could you optimize your algorithm to use only O(k) extra space?
<?php
function getRow($row) {
$result = [1,];
for ($i = 2; $i <= $row; $i ++) {
$last = 0;
for ($j = 0;$j < $i; $j ++) {
$tmp = $result[$j];
$result[$j] = $last;
$last = $tmp;
}
for ($j = 0; $j < $i; $j ++) {
$result[$j] = $result[$j] + $result[$j + 1];
}
}
print_r($result);
}
getRow(5);