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. JDK的安装与测试

    一,下载并安装JDK1.8版本以上 1.Oracle官网下载地址:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downl ...

  2. 阿里技术专家详解Dubbo实践,演进及未来规划

    https://mp.weixin.qq.com/s/9rVGHYfeE8yM2qkSVd2yEQ

  3. Mac OS X 清除DNS缓存

    参考: Flushing your DNS cache in Mac OS X and Linux Mac OS X 清除DNS缓存 根据Mac OS X操作系统的版本选择以下命令: Mac OS X ...

  4. 如何恢复IIS出厂默认设置

    How to restore IIS settings and Default Web Site? http://superuser.com/questions/704850/how-to-resto ...

  5. 数组中的stdClass Object如何访问

    使用print_r($data)输出结果为 Array ( [0] => stdClass Object ( [color_item_no] => 1 [color_name] => ...

  6. Java中的CAS实现原理

    一.什么是CAS? 在计算机科学中,比较和交换(Conmpare And Swap)是用于实现多线程同步的原子指令. 它将内存位置的内容与给定值进行比较,只有在相同的情况下,将该内存位置的内容修改为新 ...

  7. Spring中的@Async

    在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的:但是在处理与第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类任务,其实,在spring 3.x之后, ...

  8. OpenStack笔记

    *********virsh xml文件解读****************************** https://libvirt.org/format.html https://libvirt ...

  9. npm2 与 npm3的包版本管理

    npm2采用严格的包依赖模式 npm install name@1.2.* ---- 1.2.0 <= version <= 1.2.9 npm install name@1.* ---- ...

  10. jQuery中异步问题:数据传递

    最近写一个新页面,涉及到异步问题,为了获得异步过程中的数据,以下分享两种方法: 两种方法一句话总结: 方法一,Http请求后调用.then实现response的数据同步,然后根据resp接着处理: 方 ...