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. 自动化测试系列:自动化测试KPI考评的一种方法

    更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 原文链接:http://www.cnblogs.com/zishi/p/6856204.html 众所周知,在IT ...

  2. IdentityServer4支持的授权类型以及组合

    支持的授权类型: implicit hybrid authorization_code client_credentials password 支持的组合: implicit implicit,cli ...

  3. 8th,常用模块、正则表达式

    re模块 什么是正则? 正则就是用一些具有特殊含义的符号组合到一起(正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物的规则.内嵌在Python中,通过re模块实现.正则表达式模 ...

  4. lib下的Jar包在项目打包的时候提示不能找不到

    maven 使用本地包 lib jar包 依赖一个lib目录 解决方法: <plugin> <groupId>org.apache.maven.plugins</grou ...

  5. spring boot ----> 常用模板freemarker和thymeleaf

    ===========================freemarker=================================== freemarker 官网:https://freem ...

  6. VUE环境搭建、创建项目、vue调试工具

    环境搭建 第一步 安装node.js 打开下载链接:   https://nodejs.org/en/download/    这里下载的是node-v6.9.2-x64.msi; 默认式的安装,默认 ...

  7. Ubuntu用android-ndk-r15c编译boost_1_65_1

    方法一(最简单的): 下载:android-ndk-r16-beta1 然后下载Boost-for-Android:https://github.com/moritz-wundke/Boost-for ...

  8. XSS理解与防御

    一.说明 我说我不理解为什么别人做得出来我做不出来,比如这里要说的XSS我觉得很多人就不了解其定义和原理的,在不了解定义和原理的背景下他们可以拿站,这让人怎么理解呢.那时我最怕两个问题,第一个是题目做 ...

  9. JavaScript 的setTimeout 和Angular中的$timeout的區別

    JavaScript中setTimeout返回值类型和意义说明: 1.setTimeout :暂停指定的毫秒数后执行指定的代码,返回值是id标识,这个id的意义就是通过clearTimeout来清理暂 ...

  10. js获取子元素的内容

    <div class="aaa1"> <div class="">123</div> <span>2222< ...