leetcode 119. Pascal's Triangle II 帕斯卡三角形(杨辉三角)(Python)

题目:

        

Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle.

Note that the row index starts from 0.


In Pascal's triangle, each number is the sum of the two numbers directly above it.

Example:

Input: 3
Output: [1,3,3,1]

Follow up:

Could you optimize your algorithm to use only O(k) extra space?

 

代码:

from scipy.special import comb, permclass Solution:def getRow(self, rowIndex):""":type rowIndex: int:rtype: List[int]"""res = []for i in range (0,rowIndex+1):res.append(int(round(comb(rowIndex,i),0)))return res

思路:

三角中每一行数为组合中每一项的值。如第3行是:1,3,3,1。即:

调用计算排列组合的包 from scipy.special import comb, perm来计算组合的数值。

round()函数用来计算float数值四舍五入的int值的结果。

 

 


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部