Pascal's 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):
def g(ls):
res = [1]
left = 1
for i in range(1,len(ls)):
res.append(left+ls[i])
left = ls[i]
res.append(1)
return res res = []
if numRows < 3:
if numRows == 0:
return res
for i in range(1,numRows+1):
res.append([1]*i)
else:
res.append([1])
res.append([1,1]) for i in range(2,numRows):
res.append(g(res[i-1]))
return res

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,3,1].

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

class Solution:
# @return a list of integers
def getRow(self, rowIndex):
def g(ls):
res = [1]
left = 1
for i in range(1,len(ls)):
res.append(left+ls[i])
left = ls[i]
res.append(1)
return res res = []
if rowIndex< 2:
res = [1]*(rowIndex+1)
else:
res.append([1,1])
for i in range(rowIndex):
res = (g(res))
return res

LeetCode Pascal's Triangle && Pascal's Triangle II Python的更多相关文章

  1. [leetcode]Find Minimum in Rotated Sorted Array II @ Python

    原题地址:https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 解题思路:这道题和上一道题的区别是,数组中 ...

  2. 28. Triangle && Pascal's Triangle && Pascal's Triangle II

    Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...

  3. LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>

    LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...

  4. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  5. [leetcode]Word Ladder II @ Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...

  6. Leetcode之二分法专题-275. H指数 II(H-Index II)

    Leetcode之二分法专题-275. H指数 II(H-Index II) 给定一位研究者论文被引用次数的数组(被引用次数是非负整数),数组已经按照升序排列.编写一个方法,计算出研究者的 h 指数. ...

  7. Leetcode之回溯法专题-90. 子集 II(Subsets II)

    Leetcode之回溯法专题-90. 子集 II(Subsets II) 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入 ...

  8. Leetcode之回溯法专题-47. 全排列 II(Permutations II)

    Leetcode之回溯法专题-47. 全排列 II(Permutations II) 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2] ...

  9. 前端与算法 leetcode 350. 两个数组的交集 II

    目录 # 前端与算法 leetcode 350. 两个数组的交集 II 题目描述 概要 提示 解析 解法一:哈希表 解法二:双指针 解法三:暴力法 算法 # 前端与算法 leetcode 350. 两 ...

  10. 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)

    [LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

随机推荐

  1. 2017-03-03 Oracle在.Net中出现未在本地计算机上注册“OraOLEDB.Oracle”提供程序的错误

    在前面的Oracle配置完成后,打开项目运行出错,出现未在本地计算机上注册“OraOLEDB.Oracle”提供程序的错误,看到“注册”两个字,首先想到,难道还要用命令行注册一下?果不其然,需要手动注 ...

  2. English trip -- VC(情景课)3 B Bamily members

    xu言: 今天,好困啊 -__-. . zZ 早点睡吧...适当的休息,才能更好的学习 Vocabulary focus  husband wife uncle aunt brother sister ...

  3. Java开发常用Util工具类-StringUtil、CastUtil、CollectionUtil、ArrayUtil、PropsUtil

    字符串工具类 StringUtil.java package com.***.util; /** * StringUtil * @description: 字符串工具类 **/ public clas ...

  4. 我的Java学习笔记-Java面向对象

    今天来学习Java的面向对象特性,由于与C#的面向对象类似,不需详细学习 一.Java继承 继承可以使用 extends 和 implements 这两个关键字来实现继承. extends:类的继承是 ...

  5. Trailing Loves (or L'oeufs?) CodeForces - 1114C (数论)

    大意: 求n!在b进制下末尾0的个数 等价于求n!中有多少因子b, 素数分解一下, 再对求出所有素数的最小因子数就好了 ll n, b; vector<pli> A, res; void ...

  6. 『PyTorch』第十一弹_torch.optim优化器

    一.简化前馈网络LeNet import torch as t class LeNet(t.nn.Module): def __init__(self): super(LeNet, self).__i ...

  7. kill word out 1

    1● de 使~~ 成为 :离开 ,去掉,向下,变慢   2● dif 不,分开 ,否定,离开     3● deci 十分之一   4● deca 向下,离开   5● deca 十   6● di ...

  8. js 实现智能输入数字

    <!doctype html> <html> <head> <meta charset="utf-8"> <meta name ...

  9. Java通过继承外部类来建立该外部类的protected内部类的实例(转)

    原文链接:http://blog.sina.com.cn/s/blog_7de00ff60102xffx.html 如果想要在外部类的导出类(子类)中建立该外部类的为protected权限的内部类的实 ...

  10. SharePoint 解决方案和功能-PowerShell

    1. 添加解决方案到SharePoint场 Add-SPSolution "c:\newsolution.wsp" 2. 获取场中的解决方案 Get-SPSolution 3. 获 ...