LeetCode--杨辉三角

题干:

给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。

在杨辉三角中,每个数是它左上方和右上方的数的和。

示例:

输入: 5
输出:
[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]

 

解答一(思路):

1、定义一个List<List<Integer>>用来返回结果list,list用来存储杨辉三角每行的结果集。

2、list中元素的个数代表行数,每行的某个元素等于上一行的左上位+右上位之和。

优点:算法简洁明了,运用递归算法计算每个元素的值。

缺点:占用空间大,static方法返回,需要耗损较大空间,时间复杂度较大。

class Solution {
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> dataList = new ArrayList<List<Integer>>();
        if(numRows == 0){
            return dataList;
        }
        for(int i=1;i<=numRows;i++){
            List<Integer> list = new ArrayList<Integer>();
            for(int j=1;j<=i;j++){
                list.add(num(i,j));
            }
            dataList.add(list);
        }
        return dataList;
    }
    public static int num(int x,int y){
        if(y==1||y==x){
             return 1;
        }
        int c=num(x-1,y-1)+num(x-1,y);
        return c;
    }
}

解答二(LeetCode官方):

方法:动态规划

思路

如果能够知道一行杨辉三角,我们就可以根据每对相邻的值轻松地计算出它的下一行。

算法

虽然这一算法非常简单,但用于构造杨辉三角的迭代方法可以归类为动态规划,因为我们需要基于前一行来构造每一行。

首先,我们会生成整个 triangle 列表,三角形的每一行都以子列表的形式存储。然后,我们会检查行数为 00 的特殊情况,否则我们会返回 [1][1]。如果 numRows > 0numRows>0,那么我们用 [1][1] 作为第一行来初始化 triangle with [1][1],并按如下方式继续填充

class Solution {
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> triangle = new ArrayList<List<Integer>>();

        // First base case; if user requests zero rows, they get zero rows.
        if (numRows == 0) {
            return triangle;
        }

        // Second base case; first row is always [1].
        triangle.add(new ArrayList<>());
        triangle.get(0).add(1);

        for (int rowNum = 1; rowNum < numRows; rowNum++) {
            List<Integer> row = new ArrayList<>();
            List<Integer> prevRow = triangle.get(rowNum-1);

            // The first row element is always 1.
            row.add(1);

            // Each triangle element (other than the first and last of each row)
            // is equal to the sum of the elements above-and-to-the-left and
            // above-and-to-the-right.
            for (int j = 1; j < rowNum; j++) {
                row.add(prevRow.get(j-1) + prevRow.get(j));
            }

            // The last row element is always 1.
            row.add(1);

            triangle.add(row);
        }

        return triangle;
    }
}

复杂度分析

  • 时间复杂度:O(numRows^2)O(numRows2)

    虽然更新 triangle 中的每个值都是在常量时间内发生的, 但它会被执行 O(numRows^2)O(numRows2) 次。想要了解原因,就需要考虑总共有多少 次循环迭代。很明显外层循环需要运行 numRowsnumRows 次,但在外层循环的每次迭代中,内层 循环要运行 rowNumrowNum 次。因此,triangle 发生的更新总数为 1 + 2 + 3 + \ldots + numRows1+2+3+…+numRows,根据高斯公式 有

    \begin{aligned} \frac{numRows(numRows+1)}{2} &= \frac{numRows^2 + numRows}{2} \\ &= \frac{numRows^2}{2} + \frac{numRows}{2} \\ &= O(numRows^2) \end{aligned}2numRows(numRows+1)​​=2numRows2+numRows​=2numRows2​+2numRows​=O(numRows2)​

  • 空间复杂度:O(numRows^2)O(numRows2)

    因为我们需要存储我们在 triangle 中更新的每个数字, 所以空间需求与时间复杂度相同。

 

 

同名原创公众号: 程序大视界

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
LeetCode-Editor是一种在线编码工具,它提供了一个用户友好的界面编写和运行代码。在使用LeetCode-Editor时,有时候会出现乱码的问题。 乱码的原因可能是由于编码格式不兼容或者编码错误导致的。在这种情况下,我们可以尝试以下几种解决方法: 1. 检查文件编码格式:首先,我们可以检查所编辑的文件的编码格式。通常来说,常用的编码格式有UTF-8和ASCII等。我们可以将编码格式更改为正确的格式。在LeetCode-Editor中,可以通过界面设置或编辑器设置来更改编码格式。 2. 使用正确的字符集:如果乱码是由于使用了不同的字符集导致的,我们可以尝试更改使用正确的字符集。常见的字符集如Unicode或者UTF-8等。在LeetCode-Editor中,可以在编辑器中选择正确的字符集。 3. 使用合适的编辑器:有时候,乱码问题可能与LeetCode-Editor自身相关。我们可以尝试使用其他编码工具,如Text Editor、Sublime Text或者IDE,看是否能够解决乱码问题。 4. 查找特殊字符:如果乱码问题只出现在某些特殊字符上,我们可以尝试找到并替换这些字符。通过仔细检查代码,我们可以找到导致乱码的特定字符,并进行修正或替换。 总之,解决LeetCode-Editor乱码问题的方法有很多。根据具体情况,我们可以尝试更改文件编码格式、使用正确的字符集、更换编辑器或者查找并替换特殊字符等方法来解决这个问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序大视界

原创不易,请给点支持和鼓励吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值