Pascal's triangle

(1过)

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 行。

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

示例:

输入: 5
输出:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
public class PascalTriangle {
public ArrayList<ArrayList<Integer>> generate(int numRows) {
ArrayList<ArrayList<Integer>> res = new ArrayList<>();
if (numRows <= 0) {
return res;
} for (int i=0;i<numRows;i++) {
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
for (int j=1;j<=i-1;j++) {
list.add(res.get(i-1).get(j-1) + res.get(i-1).get(j));
}
if (i>=1) {
list.add(1);
}
res.add(list);
}
return res;
}
}

pascals-triangle-ii

给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 行。

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

示例:

输入: 3
输出: [1,3,3,1]

进阶:

你可以优化你的算法到 O(k) 空间复杂度吗?

    public ArrayList<Integer> getRow(int rowIndex) {
ArrayList<Integer> res = new ArrayList<>();
if (rowIndex < 0) {
return res;
}
res.add(1);
for (int i=0;i<=rowIndex;i++) {
// 关键点,从后往前遍历,从前往后的话set(j)会覆盖掉set(j+1)需要的上一行的j
for (int j=i-1;j>0;j--) {
res.set(j,res.get(j-1) + res.get(j));
}
if (i>=1) {
res.add(1);
}
}
return res;
}
}

[leetcode-118]Pascal's triangle 杨辉三角的更多相关文章

  1. [LeetCode] Pascal's Triangle 杨辉三角

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

  2. LeetCode118. Pascal's Triangle 杨辉三角

    题目 给定行数,生成对应的杨辉三角 思考 同一行是对称的,最大的下标为(行数+1)/2;1,1,2,3,6;下标从0开始,则对应分别为0.0.1.1.2.2 对于第偶数行,个数也是偶数,对于奇数行,个 ...

  3. 【LeetCode每天一题】Pascal's Triangle(杨辉三角)

    Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's t ...

  4. Pascal's Triangle(杨辉三角)

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

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

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

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

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

  7. LeetCode 118 Pascal's Triangle

    Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows  ...

  8. LN : leetcode 118 Pascal's Triangle

    lc 118 Pascal's Triangle 118 Pascal's Triangle Given numRows, generate the first numRows of Pascal's ...

  9. 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- ...

随机推荐

  1. appium 原理解析(转载雷子老师博客)

    appium 原理解析 原博客地址:https://www.cnblogs.com/leiziv5/p/6427609.html Appium是 c/s模式的appium是基于 webdriver 协 ...

  2. 微信小程序API 文档快速参考索引

    内容那么多,这个页面到底做了什么? 第一:解决微信文档APi文档使用不便: 第二:解决了内容搜索与索引:—— 最好是写成全文索引文档,但是比较需要时间,而且更新是一件麻烦的事:所以以下是直接 连接官网 ...

  3. 「HDU6158」 The Designer(圆的反演)

    题目链接多校8-1009 HDU - 6158 The Designer 题意 T(<=1200)组,如图在半径R1.R2相内切的圆的差集位置依次绘制1,2,3,到n号圆,求面积之和(n< ...

  4. C# 新语法收集

    内联变量 使用int.tryparst时,先要申明变量,用于out参数 int d; int.tryparse(s,out d); 使用内联变量写法可以如下.功能一样简化了写化 int.trypars ...

  5. python中,下级模块引用上级模块:SystemError: Parent module '' not loaded, cannot perform relative import

    当在下级中引用上级时,使用相对导包会出错,SystemError: Parent module '' not loaded, cannot perform relative import 运行test ...

  6. day15-集合

    快捷键: 先定义name,age再利用快捷键生成,Alt+shift+s +c:空参 Alt+shift+s +o:有参 Alt+shift+s +r:set&get方法Ctrl+shift+ ...

  7. 如何设计出优秀的Restful API?

    https://mp.weixin.qq.com/s?__biz=MzU0OTE4MzYzMw==&mid=2247485240&idx=1&sn=b5b9c8c41659d2 ...

  8. 【洛谷P3275】糖果

    题目大意:维护 M 个差分约束关系,问是否可以满足所有约束,如果满足输出一组解.\(N<=1e5\) 题解:差分约束模型可以通过构建一张有向图来求解.是否满足所有约束可以利用 spfa 进行判断 ...

  9. 关于 vscode 格式化自己的代码 使用shift+alt+f

    关于 vscode 格式化自己的代码 使用shift+alt+f,这样就好了

  10. python3+django2 开发易语言网络验证(下)

    第六步:网络验证服务器端项目上线部署 功夫不负有心人,终于部署成功啦! 前期准备: 项目名:netauth 系统:百度云服务器下的Ubuntu16.4 软件:xshell(无论如何想办法用这个跟服务器 ...