题目:
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]
]

翻译:
给定一个数numRows,产生前numRows行的杨辉三角(即贾宪三角形、帕斯卡三角形)。

分析:
除了每行首尾是1以外,其他元素均可由上行推出,本方案采用lastLine保存上行数据。

Java版代码:

public class Solution {public List<List<Integer>> generate(int numRows) {List<List<Integer>> result=new ArrayList<>();if(numRows<=0){return result;}List<Integer> line=new ArrayList<>();line.add(1);result.add(line);List<Integer> lastLine=line;for(int i=1;i<numRows;i++){line=new ArrayList<>();line.add(1);for(int j=1;j<i;j++){line.add(lastLine.get(j-1)+lastLine.get(j));}line.add(1);result.add(line);lastLine=line;}return result;}
}

Leet Code OJ 118. Pascal's Triangle [Difficulty: Easy]相关推荐

  1. Leet Code OJ 119. Pascal's Triangle II [Difficulty: Easy]

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

  2. Leet Code OJ 14. Longest Common Prefix [Difficulty: Easy]

    题目: Write a function to find the longest common prefix string amongst an array of strings. 翻译: 写一个函数 ...

  3. Leet Code OJ 88. Merge Sorted Array [Difficulty: Easy]

    题目: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Not ...

  4. Leet Code OJ 110. Balanced Binary Tree [Difficulty: Easy]

    题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...

  5. Leet Code OJ 326. Power of Three [Difficulty: Easy]

    题目: Given an integer, write a function to determine if it is a power of three. Follow up: Could you ...

  6. Leet Code OJ 219. Contains Duplicate II [Difficulty: Easy]

    题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...

  7. Leet Code OJ 172. Factorial Trailing Zeroes [Difficulty: Easy]

    题目: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in ...

  8. Leet Code OJ 206. Reverse Linked List [Difficulty: Easy]

    题目: Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursiv ...

  9. Leet Code OJ 38. Count and Say [Difficulty: Easy]

    题目: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 11 ...

最新文章

  1. ubuntu18上安裝TeamViewer
  2. 数据挖掘十大经典算法之——K-Means 算法
  3. 课堂笔记——Data Mining(1)
  4. bufferedreader读取中文乱码_python之pandas模块关于csv文件乱码问题解决
  5. tx1调试车辆检测遇到的问题
  6. node 安装express提示不是内部或外部命令
  7. Postgres 数据库大批量单表导入数据引发性能故障的处理
  8. JavaScript之this,new,delete,call,apply
  9. wow.js中各种特效对应的类名
  10. 不定积分/定积分——三角函数n次方不定积分公式(包含sec^nx与csc^nx不定积分公式)
  11. 甘特图首选解决方案-世界级甘特图控件(XGANTT) - XGantt甘特图中文官方网站
  12. mysql监控工具_详解MySQL监控工具 mysql-monitor
  13. 生成条码 -- jsbarcode
  14. SketchUp插件开发(一)简介
  15. 迅雷下gho文件变成php,window_GhostEXP将Windows新发布的补丁打入GHO文件,  Windows每隔一段时间,都会 - phpStudy...
  16. ubuntu 18改MAC桌面
  17. 英雄联盟——心得体会
  18. 计算机光驱运行功能,电脑如何设置光驱位第一启动项
  19. 电视节目推广方案,综艺节目推广方案怎么写
  20. 集合 Collection

热门文章

  1. 是否同一棵二叉搜索树(c语言实现)
  2. Rust学习资料大全
  3. 高性能服务器 - window篇
  4. (十)boost库之多线程
  5. 秒杀多线程第十六篇 多线程十大经典案例之一 双线程读写队列数据
  6. Python平衡点问题
  7. 微服务的下一步,离不开服务网格
  8. Go 之父:聊聊我眼中的 Go 语言和环境
  9. 使用iPhone相机和OpenCV来完成3D重建(第三部分)
  10. 音视频技术开发周刊 88期