118. 杨辉三角

给定一个非负整数numRows,生成杨辉三角的前numRows行。

![](https://www.hgnulb.cn/cnblogs-theme/image/blogimage/118-Pascal's Triangle.gif)

在杨辉三角中,每个数是它左上方和右上方的数的和。

示例

输入: 5

输出:

[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]

Java 实现

import java.util.ArrayList;
import java.util.List; class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
for(int i=1;i<=numRows;i++){
List<Integer> list = new ArrayList<Integer>();
for(int j=1;j<=i;j++){
if (j==1||j==i) { // 当列为 1,或者 行==列 的时候都为 1
list.add(1);
}else{
list.add(result.get(i-2).get(j-1)+result.get(i-2).get(j-2));
}
}
result.add(list);
}
return result;
}
}

测试代码

```java
import java.util.ArrayList;
import java.util.List;

public class Solution {

public List<List> generate(int numRows) {

List<List> result = new ArrayList<List>();

for(int i=1;i<=numRows;i++){

List list = new ArrayList();

for(int j=1;j<=i;j++){

if (j1||ji) { // 当列为 1,或者 行==列 的时候都为 1

list.add(1);

}else{

list.add(result.get(i-2).get(j-1)+result.get(i-2).get(j-2));

}

}

result.add(list);

// 测试代码

System.out.println(list);

}

return result;

}

// 测试主程序

public static void main(String[] args) {

Solution solution = new Solution();

solution.generate(5);

}

}

[1]

[1, 1]

[1, 2, 1]

[1, 3, 3, 1]

[1, 4, 6, 4, 1]

</div>

**参考资料**
* [https://leetcode.com/problems/pascals-triangle/discuss/38141/My-concise-solution-in-Java](https://leetcode.com/problems/pascals-triangle/discuss/38141/My-concise-solution-in-Java)
* [https://leetcode-cn.com/problems/pascals-triangle/description/](https://leetcode-cn.com/problems/pascals-triangle/description/)
* [https://leetcode.com/problems/pascals-triangle/discuss/?orderBy=most_votes](https://leetcode.com/problems/pascals-triangle/discuss/?orderBy=most_votes)

LeetCode 118. 杨辉三角的更多相关文章

  1. Java实现 LeetCode 118 杨辉三角

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

  2. leetcode 118. 杨辉三角(python)

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

  3. LeetCode:杨辉三角【118】

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

  4. LeetCode(119. 杨辉三角 II)

    问题描述 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 3 输出: [1,3,3,1] 进阶: 你可以优化你的 ...

  5. C语言118. 杨辉三角

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

  6. Java实现 LeetCode 119 杨辉三角 II

    119. 杨辉三角 II 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 3 输出: [1,3,3,1] 进阶: ...

  7. Leecode刷题之旅-C语言/python-118杨辉三角

    /* * @lc app=leetcode.cn id=118 lang=c * * [118] 杨辉三角 * * https://leetcode-cn.com/problems/pascals-t ...

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

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

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

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

随机推荐

  1. 安装好dashboard 登录出现错误

    验证发生错误.请稍后再试一次. While turning SELinux off certainly does the trick, it is somewhat like using a sled ...

  2. grep index.php *

    zb@zb-computer:/usr/local/nginx/conf/vhost$ grep index.php * caomall17.conf: index index.html index. ...

  3. vim 常用快捷键(整理版)

    最常用: x 删除后面的字符 X 删除前一个字符  删除3个字符就是3x dd:删除一行   D 删除到行尾 J:删除换行符,使下一行并上来.     nJ:连接后面的n行 u:撤销上一次操作     ...

  4. Vue.js和angular.js区别

    Vue.js:易学 简单 指令以v.xxx 一片HTML代码配合json,在new出来Vue,js实例 个人维护项目 适用于移动端 应用超越了angular angular.js:上手难 指令以ng. ...

  5. bzoj 1367: [Baltic2004]sequence

    1367: [Baltic2004]sequence Time Limit: 20 Sec  Memory Limit: 64 MB Description Input Output 一个整数R Sa ...

  6. [Luogu 1967] NOIP2013 货车运输

    [Luogu 1967] NOIP2013 货车运输 一年多前令我十分头大的老题终于可以随手切掉了- 然而我这码风又变毒瘤了,我也很绝望. 看着一年前不带类不加空格不空行的清纯码风啊,时光也好像回去了 ...

  7. Logitech K810 + Ubuntu

    The Logitech K810 is a nice keyboard, but it does not work with Ubuntu out of the box. Still contrar ...

  8. ASP.Net中表单POST到其他页面的方法

    在ASP中,我们通常把表单提交到另外一个页面(接受数据页面).但是在ASP.NET中,服务端表单通常都是提交到本页面的,如果我设置 form1.action="test.aspx" ...

  9. 铺地砖|状压DP练习

    有一个N*M(N<=5,M<=1000)的棋盘,现在有1*2及2*1的小木块无数个,要盖满整个棋盘,有多少种方式?答案只需要mod1,000,000,007即可. //我也不知道这道题的来 ...

  10. python学习笔记(十二)之函数

    牛刀小试: 定义一个无参函数 >>> def myFirstFunc(): ... print("Hello python") ... print("h ...