杨辉三角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]
]
构建杨辉三角,从第一行开始构建,比较简单
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> res = new ArrayList<>();
if (numRows < 1)
return res;
List<Integer> one = new ArrayList<>();
one.add(1);
res.add(one);
for (int i = 1; i < numRows; i++) {
List<Integer> cur = new ArrayList<>();
cur.add(1);
int num = 1;
while (num < i)
{
cur.add(res.get(i-1).get(num)+res.get(i-1).get(num-1));
num++;
}
cur.add(1);
res.add(cur);
}
return res;
}


杨辉三角2
Given an index k, return the kth row of the Pascal's triangle.

For example, given k = 3,
Return [1,3,3,1].

Note:
Could you optimize your algorithm to use only O(k) extra space?
要求直接输出第K行
由于有空间限制,只能在本地进行构建杨辉三角,内循环用来更新数据,外循环用来记录第几行

public  List<Integer> getRow(int rowIndex) {
List<Integer> res = new ArrayList<>();
if(rowIndex<0)
return res;
//第一行
res.add(1);
for(int i=1;i<=rowIndex;i++)
{
//从后边开始向前更新数据,第一个数不更新
for(int j=res.size()-2;j>=0;j--)
{
//当前的数加上前边的数就是下一行的当前位置数
res.set(j+1,res.get(j)+res.get(j+1));
}
//循环完后加上最后的1就是更新好了一行
res.add(1);
}
return res;
}


[leetcode]118,119PascalsTriangle,杨辉三角1,2的更多相关文章

  1. LeetCode 118:杨辉三角 II Pascal's Triangle II

    公众号:爱写bug(ID:icodebugs) 作者:爱写bug 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. Given a non-negative index k whe ...

  2. 【LeetCode】119. 杨辉三角 II Pascal‘s Triangle II(Python & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 方法一: 空间复杂度 O ( k ∗ ( k + 1 ...

  3. Pascal's Triangle leetcode java(杨辉三角)

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

  4. 【easy】118.119.杨辉三角

    这题必会啊!!! 第一题118. class Solution { public: vector<vector<int>> generate(int numRows) { ve ...

  5. LeetCode 118. 杨辉三角

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

  6. LeetCode:杨辉三角【118】

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

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

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

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

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

  9. Java实现 LeetCode 118 杨辉三角

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

随机推荐

  1. Unity使用transform.Translate()移动子物体时遇到的小问题

    Unity使用transform.Translate()移动子物体时遇到的小问题 情况大概是这样:父物体A下有子物体B,希望使B在本地坐标系下移动1单位. B物体挂脚本代码如下: transform. ...

  2. Spring Cloud 学习 (十) Spring Security, OAuth2, JWT

    通过 Spring Security + OAuth2 认证和鉴权,每次请求都需要经过 OAuth Server 验证当前 token 的合法性,并且需要查询该 token 对应的用户权限,在高并发场 ...

  3. 解决调用WebService报基础连接已经关闭: 服务器关闭了本应保持活动状态的连接的错误的方法

    问题可能原因之一:网速的快慢,我经过测试,如果外网访问的话网速慢就是出现此类问题,但是我没有精确测出当在网络流量最低在什么情况下可以避免此类问题问题可能之二:程序发布之前没把原引用的web servi ...

  4. jupyter notebook 将当前目录设置为工作目录

    生成配置文件首先打开你的CMD或者是终端(Linux),在你配置过环境变量的基础下,你直接输入以下命令: jupyter notebook --generate-config 然后打开生成的配置文件, ...

  5. PyQt(Python+Qt)学习随笔:QTableWidgetItem项whatsThis、toolTip、statusTip提示信息访问方法

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QTableWidget表格部件的QTableWidgetItem项提示信息包括工具栏提示.状态栏提 ...

  6. mysql 创建新用户、数据库、授权

    创建用户 1.登录mysql mysql -uroot -p 2.创建本地用户(2.3选其一) #use mysql;             //选择mysql数据库 #create user 'w ...

  7. git .gitignore 忽略列表

    #: 注释 # no .a files * .a    //忽略以  .a结尾的 文件 #  ... ! lib .a  //  忽略 非 lib.a的文件 /TODO  //忽略当前目录  文件名位 ...

  8. jupyterlab 增加新内核的方法ipykernel

    参考: https://blog.csdn.net/C_chuxin/article/details/82690830

  9. caffe源码 全连接层

    图示全连接层 如上图所示,该全链接层输入n * 4,输出为n * 2,n为batch 该层有两个参数W和B,W为系数,B为偏置项 该层的函数为F(x) = W*x + B,则W为4 * 2的矩阵,B ...

  10. 【题解】折纸 origami [SCOI2007] [P4468] [Bzoj1074]

    [题解]折纸 origami [SCOI2007] [P4468] [Bzoj1074] 传送门:折纸 \(\text{origami [SCOI2007] [P4468]}\) \(\text{[B ...