【LeetCode】118. Pascal's Triangle 解题报告(Python)
作者: 负雪明烛
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)的更多相关文章
- 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
Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- 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 ...
- LeetCode: Pascal's Triangle 解题报告
Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given ...
- Java [Leetcode 118]Pascal's Triangle
题目描述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...
- LeetCode 118. Pascal's Triangle (杨辉三角)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- 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 [ ...
- Java for LeetCode 118 Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
随机推荐
- mysql-select as
给查询对象起个别名. 把查询对像起个别名的作用. select ID as 用户ID,Name as 用户名 from Table_user
- 2020终于解决Chrome浏览器“崩溃啦”的问题!
Google的chrome莫名其妙突然所有页面都显示"喔唷 崩溃啦",各种插件在右下角弹出报错!这个问题我之前遇到过一次,后来通过改快捷方式的名字解决了.可是这次,隔离回来上班,打 ...
- error while loading shared libraries: libstdc++.so.5: wrong ELF class: ELFCLASS64
今天部署一个探针在运行的时候报了这样一个错:error while loading shared libraries: libstdc++.so.5: wrong ELF class: ELFCLAS ...
- LR SP PC
LR SP PC 深入理解ARM的这三个寄存器,对编程以及操作系统的移植都有很大的裨益. 1.堆栈指针r13(SP):每一种异常模式都有其自己独立的r13,它通常指向异常模式所专用的堆栈,也就是说五种 ...
- 【学相伴】Nginx最新教程通俗易懂-狂神说
Nginx - 学相伴 分享人:秦疆(遇见狂神说) 公司产品出现瓶颈? 我们公司项目刚刚上线的时候,并发量小,用户使用的少,所以在低并发的情况下,一个jar包启动应用就够了,然后内部tomcat返回内 ...
- LeetCode:旋转图像
题目描述 给定一个 n × n 的二维矩阵 matrix 表示一个图像.请你将图像顺时针旋转 90 度. 你必须在 原地 旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要 使用另一个矩阵来旋转图 ...
- [C++] vptr, where are you?
Search(c++在线运行). 有的网站很慢--不是下面的程序有问题. #include <string.h> #include <stdio.h> #include < ...
- SpringBoot之HandlerInterceptorAdapter
SpringBoot之HandlerInterceptorAdapter 在SpringBoot中我们可以使用HandlerInterceptorAdapter这个适配器来实现自己的拦截器.这样就 ...
- 二叉树——Java实现
1 package struct; 2 3 interface Tree{ 4 //插入元素 5 void insert(int value); 6 //中序遍历 7 void inOrder(); ...
- swift设置导航栏item颜色和状态栏颜色
//swift设置导航栏item颜色和状态栏颜色 let dict:Dictionary =[NSForegroundColorAttributeName:UIColor.hrgb("333 ...