【LeetCode】Pascal’s Triangle II 解题报告

标签(空格分隔): LeetCode


题目地址:https://leetcode.com/problems/pascals-triangle-ii/description/

题目描述:

Given an index k, return the kth row of the Pascal’s triangle.

For example, given k = 3,
Return [1,3,3,1].

Note:
Could you optimize your algorithm to use only O(k) extra space?

Ways

杨辉三角。这个题之前用java做过,不过现在改用python重新刷。

杨辉三角的算法是上层的两个相邻的数字相加得到该层的数字。其实可以看做下面这种方式相加:

[1]
[1,1]=[0,1]+[1,0]
[1,2,1]=[0,1,1]+[1,1,0]
[1,3,3,1]=[0,1,2,1]+[1,2,1,0]
......

即上面一层的在首尾分别添加上0之后再对应相加。这样就可以先生成后面的两个首尾加0的列表,再用列表生成式对应位置相加计算出来每层的元素。

"""@author: fuxuemingzhu"""
class Solution(object):
def getRow(self, rowIndex):
"""
:type rowIndex: int
:rtype: List[int]
"""
answer = [1]
for _ in xrange(rowIndex):
answer = [x + y for x,y in zip([0] + answer, answer + [0])]
return answer

Date

2017 年 8 月 28 日

【LeetCode】Pascal's Triangle II 解题报告的更多相关文章

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

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

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

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

  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 II 杨辉三角之二

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

  5. 【LeetCode】47. Permutations II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

  6. 【LeetCode】90. Subsets II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...

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

  8. LeetCode——Pascal's Triangle II

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

  9. 【LeetCode】275. H-Index II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/h-index- ...

随机推荐

  1. R语言中的read.table()

    参考资料:http://www.cnblogs.com/xianghang123/archive/2012/06/06/2538274.html read.table(file, header = F ...

  2. spring整合mybatis — 更新完毕

    1.准备工作 -- 导入依赖 <dependency> <groupId>org.springframework</groupId> <artifactId& ...

  3. nuxt.js相关随笔

    对于nuxt.js从未接触,对于项目需要进行零散了解,作此归纳,以下都是一个新手的拙见与理解,有不同意见欢迎提出,但请勿喷. 一.项目创建 npx create-nuxt-app projectNam ...

  4. keybd_event模拟键盘按键,mouse_event怎么用

    从 模仿UP主,用Python实现一个弹幕控制的直播间! - 蛮三刀酱 - 博客园 (cnblogs.com) 知道了 PyAutoGUI: * Moving the mouse and clicki ...

  5. 【AWS】【Basis】基础概念

    1.基础服务类型: 1.1. 链接: 官方文档,很详细:https://www.amazonaws.cn/products/#compute_networking/?nc1=f_dr 这个是一个whi ...

  6. spring boot 启动卡半天

    测试服务器到期,把环境切了,早上过来 ios 和 安卓 都说 测试环境连不上,ps -ef | grep app.jar 查看了一下进程,发现没有启动,于是 重新打包.部署,一顿骚操作后,监控启动日志 ...

  7. 【Python】【Module】os

    os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录:相当于shell下cd os.curd ...

  8. window 查看端口占用情况

    查看哪个进程在用 netstat -aon|findstr "8080" TCP    0.0.0.0:8080           0.0.0.0:0              ...

  9. html之table的tr加间隔

    <table style="border-collapse:separate; border-spacing:0px 10px;"> <tr> <td ...

  10. Spring Boot项目的不同启动方式

    方式一: 直接通过IntelliJ IDEA启动,直接执行Spring Boot项目的main()方法. 方法二: 将项目打包成jar包,首先需要在pom.xml文件的根节点下添加如下配置: < ...