Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.


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

Example:

Input: 5
Output:
[
[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]]
"""
if numRows==0:
return []
ans=[[1]]
for i in range(1,numRows):
j=0
row=[]
while j<1+i:
if j==0 or j==i:
row.append(1)
else:
row.append(ans[i-1][j-1]+ans[i-1][j])
j+=1
ans.append(row)
return ans

  

[LeetCode&Python] Problem 118. Pascal's Triangle的更多相关文章

  1. 【leetcode❤python】118. Pascal's Triangle

    #-*- coding: UTF-8 -*-#杨辉三角class Solution(object):    def generate(self, numRows):        "&quo ...

  2. LeetCode Array Easy 118. Pascal's Triangle

    Description Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. I ...

  3. 【leetcode❤python】119. Pascal's Triangle II

    #-*- coding: UTF-8 -*-#杨辉三角返回给定行#方法:自上而下考虑问题,从给定的一行计算出下一行,则给定行数之后,计算的最后一行就是求解的最后一行class Solution(obj ...

  4. leetcode 118. Pascal's Triangle 、119. Pascal's Triangle II 、120. Triangle

    118. Pascal's Triangle 第一种解法:比较麻烦 https://leetcode.com/problems/pascals-triangle/discuss/166279/cpp- ...

  5. Leetcode#118. Pascal's Triangle(杨辉三角)

    题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...

  6. LN : leetcode 118 Pascal's Triangle

    lc 118 Pascal's Triangle 118 Pascal's Triangle Given numRows, generate the first numRows of Pascal's ...

  7. 118. Pascal's Triangle

    题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...

  8. LeetCode(119) Pascal's Triangle II

    题目 Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [ ...

  9. [LeetCode]题解(python):118 Pascal's Triangle

    题目来源 https://leetcode.com/problems/pascals-triangle/ Given numRows, generate the first numRows of Pa ...

随机推荐

  1. nginx的ip_hash负载均衡配置

    upstream 4Asite{ server 192.168.16.99:8080 fail_timeout=15s; server 192.168.16.66:8080 fail_timeout= ...

  2. Windows下用python来获取微信撤回消息

    转自:https://blog.csdn.net/sunzhibin1/article/details/83348304 娱乐(windows系统) 安装itchat itchat是一个开源的pyth ...

  3. 4. Dubbo原理解析-代理之接口定义 (转)

    转载自  斩秋的专栏  http://blog.csdn.net/quhongwei_zhanqiu/article/details/41577159 一:ProxyFactory的接口定义 impo ...

  4. Codeforces 17E Palisection - Manacher

    题目传送门 传送点I 传送点II 传送点III 题目大意 给定一个串$s$询问,有多少对回文子串有交. 好像很简单的样子. 考虑能不能直接求,感觉有点麻烦.因为要考虑右端点在当前回文子串内还有区间包含 ...

  5. Java基础学习-常用的dos命令

    打开控制台(win+R,然后cmd回车)   常用命令:         d:回车    盘符切换         dir(directory):列出当前目录下的文件以及文件夹         cd( ...

  6. css的再深入7(更新中···)

    1.transparent  透明的 2.placeholder  提示语 写页面 搞清结构层次, 保证模块化,让他们之间不能受到影响 (1) 元素性质 (2) 标准流 浮动带来的脱离文档流撑不起父级 ...

  7. Python3 tkinter基础 Menu add_radiobutton 单选的下拉菜单

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  8. CF932E Team Work

    思路 第二类斯特林数和组合数推式子的题目 题目要求\(\sum_{i=1}^n \left(\begin{matrix}n \\ i \end{matrix} \right) i^k\) 一个性质 第 ...

  9. 【Git】vs code+git 不使用ssh的链接remote server的方式

    git config --global user.name "dennis wu" git config --global user.email "email" ...

  10. 导出html table 数据到Excel

    其实只需要复制  粘贴.... <script type="text/javascript" src="http://code.jquery.com/jquery- ...