leetcode @python 119. Pascal's Triangle II
2016-03-25 11:32
525 查看
题目链接
https://leetcode.com/problems/pascals-triangle-ii/题目原文
Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3,
Return
[1,3,3,1].
题目大意
给定一个整数k,返回帕斯卡三角形的第k行(序号从0开始)解题思路
和上一题类似代码
class Solution(object): def getRow(self, rowIndex): """ :type rowIndex: int :rtype: List[int] """ ans = [0] * (rowIndex + 1) for i in range(rowIndex + 1): if i == 0: ans[i] = [1] elif i == 1: ans[i] = [1, 1] else: ans[i] = [0] * (i + 1) ans[i][0] = ans[i][i] = 1 for j in range(1, i): ans[i][j] = ans[i - 1][j - 1] + ans[i - 1][j] return ans[rowIndex]
相关文章推荐
- python2.7:str unicode 详解
- leetcode @python 118. Pascal's Triangle
- 【转】python - PyDev统一编码
- python中lambda的用法
- Python常用模块用法分析
- python中FTP上传和下载
- 安装psutil模块报错&安装python-devel
- Python 解析配置模块之ConfigParser详解
- python学习(六):面向对象
- UnicodeDecodeError: 'utf8' codec can't decode byte pytho控制台输入参数编码问题
- 用Python实现斐波那契(Fibonacci)函数
- python2.7: open()文件处理使用介绍
- 备忘录 - numpy基本方法总结
- python常用函数(zip,map,filter,reduce)
- python metaclass 入门简介
- python正则表达式re匹配模式和特殊字符集
- 安装win32com报错-Python Version 2.7 required which was not found in the registry 问题解决 .
- python2.7:文件编码检测chardet 详解
- python2.7:urllib 和urllib2之间的区别
- Python基础教程之正则表达式基本语法以及re模块