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, ...
随机推荐
- 去除chrome网站https的安全检测
chrome://net-internals/#hsts 访问该网址,把要禁止检测的网址放在下面:
- java UDP 通信:服务端与客服端
import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import j ...
- 6 webpack-dev-server配置命令的第2种方式
// 导入webpack模块,这是启用热更新的第2步 const webpack=require('webpack') devServer:{ // 这是配置dev-server命令参数的第二种形式, ...
- [go] 循环与函数
练习:循环与函数 为了练习函数与循环,我们来实现一个平方根函数:用牛顿法实现平方根函数. 计算机通常使用循环来计算 x 的平方根.从某个猜测的值 z 开始,我们可以根据 z² 与 x 的近似度来调整 ...
- sqlserver内存、会话、连接查询
1.连接查询 select * from sysprocesses where dbid in (select dbid from sysdatabases where name='dbname') ...
- Greenplum 调优--数据倾斜排查(二)
上次有个朋友咨询我一个GP数据倾斜的问题,他说查看gp_toolkit.gp_skew_coefficients表时花费了20-30分钟左右才出来结果,后来指导他分析原因并给出其他方案来查看数据倾斜. ...
- package.json 与 package-lock.json 的区别
根据官方文档,这个package-lock.json 是在 `npm install`时候生成一份文件,用以记录当前状态下实际安装的各个npm package的具体来源和版本号. 它有什么用呢?因为n ...
- Educational Codeforces Round 70
目录 Contest Info Solutions A. You Are Given Two Binary Strings... B. You Are Given a Decimal String.. ...
- Bzoj 1280: Emmy卖猪pigs
1280: Emmy卖猪pigs Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 279 Solved: 182[Submit][Status][Dis ...
- IDEA正确设置编码统一为UTF-8
之前代码在myeclispe10跑得好好的来这个intellij idea 就一直出错 改了好久的编码都没卵用,如下设置才正确.还有idea的web工程目录和myeclispe的目录是不一样的,神坑. ...