1、将tri初始化为[1],当rowIndex=0时,return的结果是:1,而题目要求应该是:[1],故将tri初始化为[[1]],返回结果设置为tri[0]即可满足要求;

2、最开始第二层循环是从1到i进行遍历,这样就不好控制数据的更新,因为更新第j个数据要用到原tri行的第j-1个数据,而此时第j-1个数据已经在上一轮被更新;

3、为了解决2中的问题,将第二层循环修改为从i到1进行遍历就可以了(因为第一个元素始终为1就不需要更新了)

4、函数用法:range(start,end,step)

 class Solution:
# @return a list of integers
def getRow(self, rowIndex):
tri = [[1]]
for i in range(1,rowIndex+1):
tri[0].append(0)
for j in range(i,0,-1):
if j == i:
tri[0][j] = 1
else:
tri[0][j] += tri[0][j-1]
return tri[0]

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

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

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

  2. leetcode:Pascal's Triangle【Python版】

    1.这道题一次提交就AC了: 2.以前用C语言实现的话,初始化二维数组全部为0,然后每行第一个元素为1,只需要用a[i][j] = a[i-1][j]+a[i-1][j-1]就可以了: 3.在Pyth ...

  3. 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 ...

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

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

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

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

  6. 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 t ...

  7. [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, ...

  8. LeetCode - 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——Pascal's Triangle II

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

随机推荐

  1. 使用Jenkins实现maven项目一键部署

    下面的博客请详细的,值得一看:jenkins+maven+svn实现简单的一键发布 http://blog.csdn.net/pein_zero/article/details/52597615#co ...

  2. 3-15 《元编程》第6章 3-16 hook method

    Code That Writes Code 6.1 Coding your way to the weekend 6.2 Kernel#eval, Binding#eval Binding: Obje ...

  3. php fpm深度解析

    摘自:https://www.cnblogs.com/wanghetao/p/3934350.html 当我们在谈到cgi的时候,我们在讨论什么 最早的Web服务器简单地响应浏览器发来的HTTP请求, ...

  4. CentOS 7 Install Gitlab CE

    https://hostpresto.com/community/tutorials/how-to-install-and-setup-gitlab-on-centos-7/ http://linux ...

  5. python-day39--mysql基本操作

    1.修改密码: mysqladmin -uroot password 123 2.忘记密码如何修改密码: 1.干掉data目录---> 重新初始化   (不推荐,所有授权信息全部丢失!!!) 2 ...

  6. n阶汉诺塔 记住吧。。

    #include "bits/stdc++.h" using namespace std; int c; void move(char a,int n,char b) { prin ...

  7. bootstrap modal关闭滚动条自动会跳回最顶端问题记录

    原因:使用了a标签当按钮触发modal关闭的时候就会自动跳回浏览器最顶端 解决方案: 不要使用a标签就行了

  8. LD_PRELOAD的偷梁换柱之能

    作者: net66 原创 本文网址:http://www.cnblogs.com/net66/p/5609026.html 发布日期:2015 年 06月 22日 一.LD_PRELOAD是什么 LD ...

  9. 信号的发送kill,raise,alarm,setitimer,abort,sigqueue

    1.kill函数 int kill(pid_t pid, int sig); 发送信号给指定的进程. (1) If pid is positive, then signal sig is sent t ...

  10. 微信小程序通过js动态修改css样式的方法(交流QQ群:604788754)

    WXML <view class="page" style="background-color:{{pageBackgroundColor}}" > ...