美文网首页
php杨辉三角(leetcode)

php杨辉三角(leetcode)

作者: 王宣成 | 来源:发表于2022-01-11 17:05 被阅读0次
    image.png
    <?php
    class Solution {
    
        /**
         * @param Integer $numRows
         * @return Integer[][]
         */
        function generate($numRows) {
            if($numRows == 1){
                return [[1]];
            }
          
            $arr = [[1],[1,1]];
            if($numRows == 2){
                return $arr;
            }
    
            for($i=3;$i<=$numRows;$i++){
                $pre = $arr[$i-2];
                $temp = [1];
                for($j=1;$j<$i-1;$j++){
                    $sum = $pre[$j] + $pre[$j-1];
                    $temp[$j] = $sum;
                }
                $temp[] = 1;
                $arr[] = $temp;
            }
    
            return $arr;
    
        }
    }
    
    image.png

    相关文章

      网友评论

          本文标题:php杨辉三角(leetcode)

          本文链接:https://www.haomeiwen.com/subject/pvzbcrtx.html