1、这道题一次提交就AC了;

2、以前用C语言实现的话,初始化二维数组全部为0,然后每行第一个元素为1,只需要用a[i][j] = a[i-1][j]+a[i-1][j-1]就可以了;

3、在Python中难点应该就是每行的第一个元素和最后一个元素,最后一个元素通过判断j==i就可以区分了;

 class Solution:
# @return a list of lists of integers
def generate(self, numRows):
ret = []
for i in range(numRows):
ret.append([1])
for j in range(1,i+1):
if j==i:
ret[i].append(1)
else:
ret[i].append(ret[i-1][j]+ret[i-1][j-1])
return ret

leetcode:Pascal's Triangle【Python版】的更多相关文章

  1. [leetcode]Pascal's Triangle @ Python

    原题地址:https://oj.leetcode.com/problems/pascals-triangle/ 题意: Given numRows, generate the first numRow ...

  2. LeetCode:Pascal's Triangle I II

    LeetCode:Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For examp ...

  3. [leetcode]Pascal's Triangle II @ Python

    原题地址:https://oj.leetcode.com/problems/pascals-triangle-ii/ 题意: Given an index k, return the kth row ...

  4. LeetCode Pascal's Triangle && Pascal's Triangle II Python

    Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given ...

  5. LeetCode——Pascal's Triangle

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

  6. [LeetCode] Pascal's Triangle II 杨辉三角之二

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

  7. [LeetCode] Pascal's Triangle 杨辉三角

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

  8. 学会从后往前遍历,例 [LeetCode] Pascal's Triangle II,剑指Offer 题4

    当我们需要改变数组的值时,如果从前往后遍历,有时会带来很多麻烦,比如需要插入值,导致数组平移,或者新的值覆盖了旧有的值,但旧有的值依然需要被使用.这种情况下,有时仅仅改变一下数组的遍历方向,就会避免这 ...

  9. LeetCode - Pascal's Triangle II

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

  10. LeetCode: Pascal's Triangle II 解题报告

    Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question Solution ...

随机推荐

  1. 20170706wdVBA保存图片到本地API

    Private Type GUID Data1 As Long Data2 As Integer Data3 As Integer Data4(0 To 7) As Byte End Type Pri ...

  2. 使用dbms_output.put_line打印异常所在的行

    dbms_output.put_line(dbms_utility.format_error_stack); dbms_output.put_line(dbms_utility.format_call ...

  3. nodejs初探一二

    概念 简单来说,node.js 是一个让 JavaScript 运行在服务端的开发平台,让开发者通过JS编写服务端程序. 安装 从nodeJS官网下载对应平台的安装程序,安装完成后,打开命令行工具,然 ...

  4. 微信小程序wx.chooseImage和wx.previewImage的综合使用(图片上传不限制最多张数)

    WXML: <view class="weui"> <view class="weui-uploader"> <view clas ...

  5. Redis (一) 概念安装

    一.阿里云安装Redis 1.安装Redis yum -y install redis 2.启动Redis service redis start 或者(推荐使用) systemctl start  ...

  6. stringBuild置空方法

    参看连接:http://blog.csdn.net/roserose0002/article/details/6972391

  7. window上创建python3虚拟环境

    虚拟环境,就是为某个需要单独运行的软件创建一个隔绝的环境,虚拟程序中运行的程序不会影响电脑上其他软件的运行.例如同时使用python2和python3,可以在两个不同的虚拟环境中分别运行. 安装虚拟环 ...

  8. ORA-01034:Oracle not available

    ORA-01034:Oracle not available 问题描述:ora-01034常与ora-27101同时出现,都是在登录数据库的时候报该错误 错误原因:出现ORA-01034和ORA-27 ...

  9. L1-012 计算指数

    真的没骗你,这道才是简单题 —— 对任意给定的不超过 10 的正整数 n,要求你输出 2​n​​.不难吧? 输入格式: 输入在一行中给出一个不超过 10 的正整数 n. 输出格式: 在一行中按照格式  ...

  10. ESET Smart Security 6 – 免费60天(SG)

    ESS 60天 - 活动消息来自新加坡脸谱https://www.facebook.com/esetsingapore/app_190322544333196IP限制Sg,suiss提供个在线代理ht ...