【leetcode❤python】119. Pascal's Triangle II
#-*- coding: UTF-8 -*-
#杨辉三角返回给定行
#方法:自上而下考虑问题,从给定的一行计算出下一行,则给定行数之后,计算的最后一行就是求解的最后一行
class Solution(object):
def getRow(self, rowIndex):
"""
:type rowIndex: int
:rtype: List[int]
"""
#初始化杨辉三角前两行
ans=[[1],[1,1]]
#如果输入rowIndex小于2 表示的是前两行
if rowIndex<2:return ans[rowIndex]
#以初始化的杨辉三角为第一个一个pre=ans[-1]
pre=ans[-1]
#i表示的是从第一行开始计算一直计算到rowIndex-1行,i=1可以计算出i=2
for i in range(1,rowIndex):
print i, pre
# t 表示计算的是下一行元素的 除去首尾两个的其他元素值
nextT=[1]+[pre[t]+pre[t+1] for t in range(len(pre)-1)]+[1]
pre=nextT
return pre
【leetcode❤python】119. Pascal's Triangle II的更多相关文章
- 【leetcode❤python】118. Pascal's Triangle
#-*- coding: UTF-8 -*-#杨辉三角class Solution(object): def generate(self, numRows): "&quo ...
- 【一天一道LeetCode】#119. Pascal's Triangle II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】119. Pascal's Triangle II
题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [ ...
- LeetCode Array Easy 119. Pascal's Triangle II
Description Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's tria ...
- LeetCode OJ 119. 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 118. Pascal's Triangle 、119. Pascal's Triangle II 、120. Triangle
118. Pascal's Triangle 第一种解法:比较麻烦 https://leetcode.com/problems/pascals-triangle/discuss/166279/cpp- ...
- 118/119. Pascal's Triangle/II
原文题目: 118. Pascal's Triangle 119. Pascal's Triangle II 读题: 杨辉三角问题 '''118''' class Solution(object): ...
- 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 ...
- [LeetCode]题解(python):119 Pascal's Triangle II
题目来源 https://leetcode.com/problems/pascals-triangle-ii/ Given an index k, return the kth row of the ...
随机推荐
- 导出Excel offer2007以上
package cn.knet.data.untils; import java.awt.Color; import java.io.File; import java.io.FileOutputSt ...
- json转化为java实体
import net.sf.json.JSONObject; Map<String, Object> classMap = new HashMap<String, Object> ...
- 项目中empty遇到的一个问题
搜索传参时,数据能获取到,搜索结果不是根据参数得出的,在定义搜索条件时因为empty引起的一个问题,键为0的值没有获取到, 记住:!empty 已经把0排除了
- django migrate10060 Duplicate column name错误
这个错误意思是有重复的列名,其实大部分原因是因为某些列被执行了多次,可以打开migration里面的django生成的文件去排查错误,然后自己手动修改数据库还原,实在不行可以把除了0001和init文 ...
- 分页实体类:PageBean
package com.eaju.soms.entity.custom; import java.util.List; @SuppressWarnings("rawtypes")p ...
- 【python cookbook】【字符串与文本】6.以不区分大小写的方式对文本做查找和替换
问题:以不区分大小写的方式对文本做查找和替换 解决方法:使用re模块,并对各种操作都添加上re.IGNORECASE标记 text='UPPER PYTHON,lower python,Mixed P ...
- React笔记_(7)_react路由
路由 路由(routing)是指分组从源到目的地时,决定端到端路径的网络范围的进程. 路由器当然是作为一个转发设备出现的,主要是转发数据包来实现网络互联. 那么react的路由到底指的是什么呢? 举个 ...
- python 读取全国城市aqi数据,差值生成png图片
# -*- coding: utf-8 -*- import arcpy import sys import datetime import cx_Oracle import json import ...
- Java中如何在另一个类里面使用运行类中的对象,举例说明了一下。
package 计时器; import java.util.Timer; import java.util.TimerTask; /* * 主要是想在另一个类里面,使用该类的对象,如何使用呢?如何传递 ...
- 这个Glance的界面该怎么看出问题,为什么状态是SOCKT?
这个glance的状态图有问题吗?