LeetCode_118. Pascal's Triangle
118. Pascal's Triangle
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]
]
package leetcode.easy; import java.util.ArrayList;
import java.util.List; public class PascalSTriangle {
@org.junit.Test
public void test() {
System.out.println(generate(5));
} public List<List<Integer>> generate(int numRows) {
List<List<Integer>> triangle = new ArrayList<List<Integer>>(); // First base case; if user requests zero rows, they get zero rows.
if (numRows == 0) {
return triangle;
} // Second base case; first row is always [1].
triangle.add(new ArrayList<Integer>());
triangle.get(0).add(1); for (int rowNum = 1; rowNum < numRows; rowNum++) {
List<Integer> row = new ArrayList<>();
List<Integer> prevRow = triangle.get(rowNum - 1); // The first row element is always 1.
row.add(1); // Each triangle element (other than the first and last of each row)
// is equal to the sum of the elements above-and-to-the-left and
// above-and-to-the-right.
for (int j = 1; j < rowNum; j++) {
row.add(prevRow.get(j - 1) + prevRow.get(j));
} // The last row element is always 1.
row.add(1); triangle.add(row);
} return triangle;
}
}
LeetCode_118. Pascal's Triangle的更多相关文章
- [LeetCode] 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] Pascal's Triangle 杨辉三角
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- 【leetcode】Pascal's Triangle II
题目简述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Retur ...
- 【leetcode】Pascal's Triangle
题目简述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...
- LeetCode 118 Pascal's Triangle
Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- LeetCode 119 Pascal's Triangle II
Problem: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Ret ...
- LeetCode - Pascal's Triangle II
题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return ...
- 【leetcode】Pascal's Triangle I & II (middle)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- 118. Pascal's Triangle
题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...
随机推荐
- 优化你的HTTPS(上),你需要这么做
HTTP/2 HTTP 2.0即超文本传输协议 2.0,是下一代HTTP协议.是由互联网工程任务组(IETF)的Hypertext Transfer Protocol Bis (httpbis)工作小 ...
- Educational Codeforces Round 75 (Rated for Div. 2) A. Broken Keyboard
链接: https://codeforces.com/contest/1251/problem/A 题意: Recently Polycarp noticed that some of the but ...
- MySQL数据库中常用的引擎有几种?有什么区别?
1.常用的3种 2.InnoDB Myisam Memory 3.InnoDB跟Myisam的默认索引是B+tree,Memory的默认索引是hash 区别: 1.InnoDB支持事务,支持外键,支 ...
- $("xxx"); 使用jQuery获得对象
$("xxx"); 使用jQuery获得对象 console.log( $ ( $ ( $ ( '.dropdown' ) ) ) ); 与下面返回的结果都是一毛一样的. con ...
- hive表的DDL
查看表 hive> show tables;创建表 hive> create table t1(id int);查看表结构 ...
- 学习了武沛齐讲的Day10-完
int 整形 int 将字符串转换为数字 x.bit_length() ===== 当前数字的二进制,至少用n位表示 str 字符串 x.capitalize()== ...
- leetcode解题报告(21):Majority Element
描述 Given an array of size n, find the majority element. The majority element is the element that app ...
- SONiC项目的发展及其相关介绍(转载)
SONiC作为一个开源项目,理论上是包含了SAI(switch abstraction interface,交换机抽象接口),SAI是没有开源的,厂商自己完成统一的API,提供给上层sonic用户来调 ...
- QTTabBar 使用
QTTabBar 激活 修改语言
- Tkinter 之ListBox列表标签
一.参数说明 参数 作用 background (bg) 设置背景颜色 borderwidth (bd) 指定 Listbox 的边框宽度,通常是 2 像素 cursor 指定当鼠标在 Listbo ...