Created
September 15, 2019 23:14
-
-
Save dasider41/7f503ba900618c92b507b8c84f78fd78 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
In Pascal's triangle, each number is the sum of the two numbers directly above it. | |
Example: | |
Input: 5 | |
Output: | |
[ | |
[1], | |
[1,1], | |
[1,2,1], | |
[1,3,3,1], | |
[1,4,6,4,1] | |
] | |
*/ | |
/** | |
* @param Integer $numRows | |
* @return Integer[][] | |
*/ | |
function generate(int $numRows): array | |
{ | |
$result = []; | |
for ($i = 0; $i < $numRows; $i++) { | |
$temp[0] = 1; | |
for ($j = 1; $j < $i; $j++) { | |
$temp[$j] = $result[$i - 1][$j - 1] + $result[$i - 1][$j]; | |
} | |
$temp[$i] = 1; | |
$result[$i] = $temp; | |
} | |
return $result; | |
} | |
var_dump(generate(5)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment