Leetcode基础刷题之PHP解析(118. Pascal's Triangle)

栏目: PHP · 发布时间: 6年前

内容简介:如果当前没有针对性的话,那就从头把之前补上。

2 0 1 9 -5 -28   期二    

如果当前没有针对性的话,那就从头把之前补上。

Leetcode基础刷题之 PHP 解析(66. Plus One)

Leetcode基础刷题之PHP解析(118. Pascal's Triangle)

给定一个行数,生成杨辉三角形。

生成这样的三角形是有规律的,每一行的首尾的值都是1,从第三行开始,中间的每一位数都是前一行左右数之和。

/**
     * @param Integer $numRows
     * @return Integer[][]
     */
    function generate($numRows) {
       $res=[];
        for($i=0;$i<$numRows;$i++){
            for($j=0;$j<=$i;++$j){
                if($i==0 && $j==0) $res[0][0]=1;
                else $res[$i][$j]=$res[$i-1][$j-1]+$res[$i-1][$j];  
            }
        }
        return $res;
    }

Github整理地址: https://github.com/wuqinqiang/leetcode-php


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Everything Store

The Everything Store

Brad Stone / Little, Brown and Company / 2013-10-22 / USD 28.00

The definitive story of Amazon.com, one of the most successful companies in the world, and of its driven, brilliant founder, Jeff Bezos. Amazon.com started off delivering books through the mail. Bu......一起来看看 《The Everything Store》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试