118_杨辉三角与杨辉三角II

118.杨辉三角

给定一个非负整数 numRows,生成「杨辉三角」的前 numRows 行。
在「杨辉三角」中,每个数是它左上方和右上方的数的和。

示例 1:
输入: numRows = 5
输出: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]

解法一:动态规划
dp[i][j] = dp[i-1][j-1] + [i-1][j]

class Solution:
    def generate(self, numRows: int) -> List[List[int]]:

        res = []
        for i in range (numRows):
            temp = []
            for j in range(0,i+1):
                if j == 0 or j == i:
                    temp.append(1)
                else:
                    temp.append(res[i-1][j-1] + res[i-1][j])
            
            res.append(temp)
               
        return res 

解法二:错位逐个相加
在这里插入图片描述

class Solution:
    def generate(self, numRows: int) -> List[List[int]]:

        if numRows == 0:
            return []

        res = [[1]]
        for i in range(numRows-1):
            temp = [a+b for a,b in zip([0] + res[-1],res[-1] + [0])]
            res.append(temp)
        
        return res 

zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。
如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用 * 号操作符,可以将元组解压为列表。

zip 方法在 Python 2 和 Python 3 中的不同:在 Python 3.x 中为了减少内存,zip() 返回的是一个对象。如需展示列表,需手动 list() 转换。

解法二更精简的代码:
Python2版本

class Solution(object):
    def generate(self, numRows):
        """
        :type numRows: int
        :rtype: List[List[int]]
        """
        res = [[1]]
        for i in range(1, numRows):
            res += [map(lambda x, y: x+y, res[-1] + [0], [0] + res[-1])]
        return res[:numRows]

Python3版本

class Solution:
    def generate(self, numRows: int) -> List[List[int]]:
        res = [[1]]
        for i in range(1, numRows):
            res += [list(map(lambda x, y: x+y, res[-1] + [0], [0] + res[-1]))]
        return res[:numRows]

对于map()函数:
Python 2.x 返回列表。
Python 3.x 返回迭代器。
lambda函数→匿名函数

解法三(一维dp):

class Solution:
    def generate(self, numRows: int) -> List[List[int]]:

        ans = []
        for i in range(numRows):
            if i == 0:
                k = [1]
            elif i == 1:
                k = [1,1]
            else:
                p = [1,1]
                for j in range(1,i):
                    p.insert(j,k[j-1] + k[j])     # insert(index,object)
                
                k = p[:]
            ans.append(k)
        
        return ans

119.杨辉三角II
给定一个非负索引 rowIndex,返回「杨辉三角」的第 rowIndex 行。
在「杨辉三角」中,每个数是它左上方和右上方的数的和。

示例1:
输入: rowIndex = 3
输出: [1,3,3,1]

示例2:
输入: rowIndex = 0
输出: [1]

直接上代码
解法一(错位相加,返回最后一行):

class Solution(object):
    def getRow(self, rowIndex):
        """
        :type rowIndex: int
        :rtype: List[int]
        """
        res = [[1]]
        rowIndex += 1
        for i in range(1,rowIndex):
            res += [list(map(lambda x, y: x+y, res[-1] + [0], [0] + res[-1]))]

        return res[-1]

解法二(每次更新当前数组,解法二的一维dp):

class Solution(object):
    def getRow(self, rowIndex):
        """
        :type rowIndex: int
        :rtype: List[int]
        """

        res = [1]
        for i in range(1,rowIndex+1):
            res.insert(0,0)  # 使用insert方法
            for j in range(i):
                res[j] += res[j+1]

        return res
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值