zoukankan      html  css  js  c++  java
  • Pascal's Triangle

    Given numRows, generate the first numRows of Pascal's triangle.

    For example, given numRows = 5,
    Return

    [
         [1],
        [1,1],
       [1,2,1],
      [1,3,3,1],
     [1,4,6,4,1]
    ]

    简单题,不过却有解法上还是有多种的。首先给出我简单直接的解法:

    class Solution(object):
        def generate(self, numRows):
            """
            :type numRows: int
            :rtype: List[List[int]]
            """
            ret = []
            for i in range(numRows):
                ret.append([1])
                for j in range(1,i):
                    ret[i].append(ret[i-1][j]+ret[i-1][j-1])
                if i >0:
                    ret[i].append(1)
            return ret

    主要就是每行处理,将开始的1和最末端的1单独加上。时间复杂度为O(n^2),在结果上直接进行处理,不需要额外空间。

  • 相关阅读:
    vue基础知识
    git的创建使用
    使用express搭建服务器框架
    日常训练
    今日收获
    今日收获
    今日收获
    今日收获
    今日收获
    今日收获
  • 原文地址:https://www.cnblogs.com/sherylwang/p/5448828.html
Copyright © 2011-2022 走看看