leetcode-easy-others-118 Pascal's Triangle
mycode 16.47%
class Solution(object):
def generate(self, numRows):
"""
:type numRows: int
:rtype: List[List[int]]
"""
if not numRows : return []
elif numRows == 1 : return [[1]]
res = [[1],[1,1]]
for i in range(2,numRows):
temp = [1]
for j in range(1,i):
temp.append(res[i-1][j-1] + res[i-1][j])
temp.append(1)
res.append(temp)
return res
参考
class Solution(object):
def generate(self, numRows):
"""
:type numRows: int
:rtype: List[List[int]]
"""
triangle=[[1]*n for n in range(1,numRows+1)] for i in range(2,numRows): #第三行开始
for j in range(1,i): #第二列到倒数第二列
triangle[i][j]=triangle[i-1][j-1]+triangle[i-1][j]
return triangle
leetcode-easy-others-118 Pascal's Triangle的更多相关文章
- [LeetCode&Python] Problem 118. Pascal's Triangle
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's t ...
- 【leetcode❤python】118. Pascal's Triangle
#-*- coding: UTF-8 -*-#杨辉三角class Solution(object): def generate(self, numRows): "&quo ...
- 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- ...
- Leetcode#118. Pascal's Triangle(杨辉三角)
题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...
- LN : leetcode 118 Pascal's Triangle
lc 118 Pascal's Triangle 118 Pascal's Triangle Given numRows, generate the first numRows of Pascal's ...
- 118. Pascal's Triangle
题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...
- 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 118. Pascal's Triangle
Description Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. I ...
- 【LeetCode】118. Pascal's Triangle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- LeetCode 118 Pascal's Triangle
Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
随机推荐
- SSM处理 No 'Access-Control-Allow-Origin' header is present on the requested resource 问题
在开发中,前端同事调用后端同事写好的接口,在地址中是有效的,但在项目的ajax中,浏览器会报 "No 'Access-Control-Allow-Origin' header is pres ...
- Delphi FileListBox组件
- 垃圾回收gc --翻译
原文在https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management.基本保持了平译,并在一些地方做了概念解释.(转 ...
- [牛客] [# 1108 E] Grid
2019牛客国庆集训派对day3 链接:https://ac.nowcoder.com/acm/contest/1108/E来源:牛客网 题意 在一个$10 ^ 9 * 10 ^ 9$ 的方格中,每次 ...
- 1.Go-copy函数、sort排序、双向链表、list操作和双向循环链表
1.1.copy函数 通过copy函数可以把一个切片内容复制到另一个切片中 (1)把长切片拷贝到短切片中 ? 1 2 3 4 5 6 7 8 9 10 11 12 package main imp ...
- C语言结构体初始化的四种方法
定义 struct InitMember{ int first: double second: char* third: float four;}; 方法一:定义时赋值 str ...
- 【鸽】poj3311 Hie with the Pie[状压DP+Floyd]
题解网上一搜一大坨的,不用复述了吧. 只是觉得网上dp方程没多大问题,但是状态的表示含义模糊.不同于正常哈密顿路径求解,状态表示应当改一下. 首先定义一次移动为从一个点经过若干个点到达另一个点,则$f ...
- qt5--自定义控件封装
视频教程地址:https://www.bilibili.com/video/av51766541/?p=30
- head first 设计模式笔记1-策略模式:模拟鸭子
1.第一个设计原则:找出应用中可能需要变化之处,把它们独立出来,不要和那些不需要变化的代码混在一起.该原则几乎是所有设计模式背后的精神所在. 这个原则的另一种思考方式:把会变化的部分取出并封装起来,以 ...
- 《剑指offer》算法题第二天
今日题目(分别对应剑指书3~9题): 数组中重复的数字 二维数组中的查找 替换空格 从尾到头打印链表 重建二叉树 二叉树的下一个节点 用两个栈实现队列 今日重点为1,2,5,6,后面会有详细的思路解析 ...