[leetcode]118,119PascalsTriangle,杨辉三角1,2
杨辉三角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的更多相关文章
- LeetCode 118:杨辉三角 II Pascal's Triangle II
公众号:爱写bug(ID:icodebugs) 作者:爱写bug 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. Given a non-negative index k whe ...
- 【LeetCode】119. 杨辉三角 II Pascal‘s Triangle II(Python & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 方法一: 空间复杂度 O ( k ∗ ( k + 1 ...
- Pascal's Triangle leetcode java(杨辉三角)
题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...
- 【easy】118.119.杨辉三角
这题必会啊!!! 第一题118. class Solution { public: vector<vector<int>> generate(int numRows) { ve ...
- LeetCode 118. 杨辉三角
118. 杨辉三角 给定一个非负整数numRows,生成杨辉三角的前numRows行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例 输入: 5 输出: [ [1], [1,1], [1,2 ...
- LeetCode:杨辉三角【118】
LeetCode:杨辉三角[118] 题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: ...
- LeetCode 118. Pascal's Triangle (杨辉三角)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- Leetcode#118. Pascal's Triangle(杨辉三角)
题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...
- Java实现 LeetCode 118 杨辉三角
118. 杨辉三角 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], ...
随机推荐
- Maven 依赖树的解析规则
对于 Java 开发工程师来说,Maven 是依赖管理和代码构建的标准.遵循「约定大于配置」理念.Maven 是 Java 开发工程师日常使用的工具,本篇文章简要介绍一下 Maven 的依赖树解析. ...
- LeetCode 025 Reverse Nodes in k-Group
题目描述:Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time an ...
- LeetCode 018 4Sum
题目描述:4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c ...
- AndroidStudio中默认不导入org.apache.http等包的解决方法
参考:http://www.cnblogs.com/xiadongqing/p/5942459.html Eclipse ADT中默认引入了org.apache.http包,而AndroidStudi ...
- Django之数据库--ORM
一.建立数据库模型类 1.在model里创建模型类.(继承models.Model) from django.db import models # Create your models here. c ...
- 2017 Mid Central Regional G.Hopscotch (组合计数)
这道题有点意思,给出点(N,N),你在原点处向目标点走,每次只能向x和y两个方向走路,每次xy两个方向的步幅分别不能小于dx和dy,问走到终点的方案数,答案对1e9 + 7取模 这道题最直接的想法就是 ...
- GPU相关资料汇总
qemu, quick emulator systemc xilinx qemu nvdla, nvidia deep learning accelerator gpgpu-sim ffgpu ope ...
- LSB隐写加密MISC
没有做过LSB隐写加密的题目,在buuoj上面做到了就记录一下,估计后面很长的时间都会在这个平台上面训练自己的MISC和WEB,是很好的平台,把很多比赛的原题和安恒的周赛的复现了. 题目是MISC里面 ...
- 再也不怕 JavaScript 报错了,怎么看怎么处理都在这
在开发中,有时,我们花了几个小时写的 JS 代码,在游览器调试一看,控制台一堆红,瞬间一万头草泥马奔腾而来.至此,本文主要记录 JS 常见的一些报错类型,以及常见的报错信息,分析其报错原因,并给予处理 ...
- Java中的Reference类使用
Java 2 平台引入了 java.lang.ref 包,这个包下面包含了几个Reference相关的类,Reference相关类将Java中的引用也映射成一个对象,这些类还提供了与垃圾收集器(gar ...