[leetcode]Pascal's Triangle @ Python
原题地址:https://oj.leetcode.com/problems/pascals-triangle/
题意:
Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
Return
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
解题思路:杨辉三角的求解。
代码:
class Solution:
# @return a list of lists of integers
def generate(self, numRows):
if numRows == 0:
return []
if numRows == 1:
return [[1]]
if numRows == 2:
return [[1], [1, 1]]
if numRows > 2:
list = [[] for i in range(numRows)]
for i in range(0, numRows):
list[i] = [1 for j in range(i + 1)]
for i in range(2, numRows):
for j in range(1, i):
list[i][j] = list[i - 1][j - 1] + list[i - 1][j]
return list
[leetcode]Pascal's Triangle @ Python的更多相关文章
- LeetCode:Pascal's Triangle I II
LeetCode:Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For examp ...
- [leetcode]Pascal's Triangle II @ Python
原题地址:https://oj.leetcode.com/problems/pascals-triangle-ii/ 题意: Given an index k, return the kth row ...
- 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 ...
- LeetCode——Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- [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, ...
- [LeetCode] Pascal's Triangle 杨辉三角
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- 学会从后往前遍历,例 [LeetCode] Pascal's Triangle II,剑指Offer 题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 ...
- LeetCode: Pascal's Triangle II 解题报告
Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question Solution ...
随机推荐
- Android-Kotlin在Fragment获取View
Android-Kotlin在Fragment获取View Overview 在使用Fragment的时候,使用了ButterKnife 来获取View但是一直出错,后来就直接使用Kotlin的导入布 ...
- IE9中ajax请求成功后返回值却是undefined
ie9中ajax请求一般处理程序成功后返回值始终是undefined,在网上找过很多资料,大致意思都是说前后端编码不一致造成的,但是按照资料上的方案去修改却发现根本不能解决我的问题,试过好多种方案都不 ...
- hdu 1241Oil Deposits(dfs模板)
题目链接—— http://acm.hdu.edu.cn/showproblem.php?pid=1241 首先给出一个n*m的字符矩阵,‘*’表示空地,‘@’表示油井.问在这个矩阵中有多少组油井区? ...
- 没用过的函数 GetHomePath - 获取用户程序数据目录
uses System.IOUtils; procedure TForm1.FormCreate(Sender: TObject); var S: string; begin { 三种方法结果一致: ...
- Mac的brew和brew cask区别以及安装brew cask
brew多用于命令行. brew cask主要用于有GUI的软件,例如VLC等等. brew cask是brew的一个子集,也就是一个扩展. 安装brew cask扩展: ruby -e " ...
- How to tell if a file is an EXE or a DLL?
How to tell if a file is an EXE or a DLL? void DumpFile(LPWSTR filename) { HANDLE hFile = CreateFile ...
- How to create .gitignore file in Windows Explorer
How to create .gitignore file I need to add some rules to my .gitignore file, however, I can't find ...
- java系列之 原生数据类型
在我看来,java里面里面除了原生类型不是对象,其他的都是对象.但java是面向对象的语言,很多地方还要要操作对象,所以java会自动把原生类型转为对应的包装类型.这个过程叫自动装箱.有装箱就有拆箱, ...
- 自定义两行可左右滑动的GridView
效果图: xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...
- Silverlight for Windows Phone Toolkit
Silverlight Toolkit 是一套codeplex上的很好的代码库,它里面包含了很多常用的但是Silverlight本身并不包含的控件.文档等内容.现在Silverlight Toolki ...