作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


[LeetCode]

题目地址:https://leetcode.com/problems/pascals-triangle/

Total Accepted: 83023 Total Submissions: 247907 Difficulty: Easy

题目描述

Given a non-negative integer numRows, generate the first numRows of Pascal’s triangle.

In Pascal’s triangle, each number is the sum of the two numbers directly above it.

Example:

Input: 5
Output:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]

题目大意

杨辉三角。

解题方法

Java解法

智商呀!智商!大一的题竟然纠结的有一个小时!

最后找到错误的根源在于第10行的temp=new ArrayList();之前写的是temp.clear();
这样的问题是clear()之后那个temp还在用,就是说如果下次修改temp的话会把原来已经放进去的给改了。

所以java基础很重要!

public class Solution {
public List<List<Integer>> generate(int numRows) {
if(numRows<=0) return new ArrayList();
List<List<Integer>> answer=new ArrayList();
List<Integer> temp=new ArrayList();
temp.add(1);
answer.add(temp);
if(numRows==1) return answer;
for(int i=2;i<=numRows;i++){
temp=new ArrayList();
for(int j=0;j<i;j++){
if(j==0 || j==i-1){
temp.add(1);
}else{
temp.add(answer.get(i-2).get(j-1) + answer.get(i-2).get(j));
}
}
answer.add(temp);
}
return answer;
}
}

AC:2ms

Python解法

二刷的时候采用的Python解法,而且我一遍就AC了,看到了两年前的答案还是觉得自己进步了的。

使用的方法是提前构造好三角形,然后再遍历每行的中间位置是上面两行的和即可。

class Solution(object):
def generate(self, numRows):
"""
:type numRows: int
:rtype: List[List[int]]
"""
res = [[1 for j in range(i)] for i in range(1, numRows + 1)]
for i in range(2, numRows):
for j in range(1, i):
res[i][j] = res[i - 1][j - 1] + res[i - 1][j]
return res

日期

2016 年 05月 8日
2018 年 11 月 19 日 —— 周一又开始了

【LeetCode】118. Pascal's Triangle 解题报告(Python)的更多相关文章

  1. 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- ...

  2. LeetCode 118 Pascal's Triangle

    Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows  ...

  3. Leetcode#118. Pascal's Triangle(杨辉三角)

    题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...

  4. LN : leetcode 118 Pascal's Triangle

    lc 118 Pascal's Triangle 118 Pascal's Triangle Given numRows, generate the first numRows of Pascal's ...

  5. LeetCode: Pascal's Triangle 解题报告

    Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given ...

  6. Java [Leetcode 118]Pascal's Triangle

    题目描述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...

  7. LeetCode 118. Pascal's Triangle (杨辉三角)

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  8. leetcode 【 Pascal's Triangle II 】python 实现

    题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [ ...

  9. Java for LeetCode 118 Pascal's Triangle

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...

随机推荐

  1. os.path.join()函数

    连接两个或更多的路径名组件 import os p1 = '/date' p2 = 'mage' p3 = 'img' all = os.path.join(p1,p2,p3) print(all) ...

  2. Linux实现批量添加用户及随机密码小脚本

    通过chpasswd命令可实现迅速为用户批量设置密码     实例:写一个脚本,实现批量添加20个用户user1-20,密码为用户名和后面跟5个随机字符 #!/bin/sh # 思路:通过for循环, ...

  3. 【系统硬件】英伟达安培卡 vs 老推理卡硬件参数对比

      欢迎关注我的公众号 [极智视界],回复001获取Google编程规范   O_o   >_<   o_O   O_o   ~_~   o_O   本文分享一下英伟达安培卡 vs 老推理 ...

  4. 搭建简单的SpringCloud项目三:问题及解决

    GitHub:https://github.com/ownzyuan/test-cloud 前篇:搭建简单的SpringCloud项目一:注册中心和公共层 搭建简单的SpringCloud项目二:服务 ...

  5. 在C++的map类型中按value排序

    1.将map转化为vector类型 2.使用sort函数对vector进行排序,写出compare比较器函数 3.比较器中指明按照第几个元素来排序 1 #include <iostream> ...

  6. accessory, accident

    accessory 1. belt, scarf, handbag, Penny用rhinestone做的小首饰(Penny Blossom)都是accessory2. With default se ...

  7. Learning Spark中文版--第五章--加载保存数据(2)

    SequenceFiles(序列文件)   SequenceFile是Hadoop的一种由键值对小文件组成的流行的格式.SequenceFIle有同步标记,Spark可以寻找标记点,然后与记录边界重新 ...

  8. django数据库增删改查

    django中数据库基本操作: 1.同步数据库 python manage.py makemigrations #生成migrations python manage.py migrate #应用mi ...

  9. 2019广东工业大学新生杯决赛 I-迷途的怪物

    题目:I-I-迷途的怪物_2019年广东工业大学腾讯杯新生程序设计竞赛(同步赛) (nowcoder.com) 将(p-1)^n 按照多项式定理拆开,会发现只有一项没有p,其余项都有p,可直接约掉. ...

  10. k8s配置中心-configmap,Secret密码

    目录 k8s配置中心-configmap,Secret 创建ConfigMap 使用ConfigMap subPath参数 Secret 官方文档 编写secret清单 使用secret 在 Pod ...