[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], ...
随机推荐
- python之迭代器,生成器小结
1.凡是可作用于for循环的对象都是Iterable类型: 2.凡是可作用于next()函数的对象都是Iterator类型,它们表示一个惰性计算的序列: 3.集合数据类型如list.dict.str等 ...
- spring-boot-starter
Spring Boot Starter 是在 SpringBoot 组件中被提出来的一种概念,stackoverflow 上面已经有人概括了这个 starter 是什么东西,想看完整的回答戳 这里. ...
- python初次接触
1.python有什么用或者能做什么? 可以做网站(比如 YouTube.豆瓣),可以做图片处理,可以做科学计算,也可以爬虫,甚至于游戏,学好Python后不用担心没有用武之地,Google就大量的在 ...
- THE BUG 队第一次团队项目作业
队名: THE BUG 队 2.队员学号: 杨梓琦 3118005115(队长) 温海源,3118005109 陈杰才,3118005089 李华,3118005097 钟明康,3118005123 ...
- [从源码学设计]蚂蚁金服SOFARegistry之存储结构
[从源码学设计]蚂蚁金服SOFARegistry之存储结构 目录 [从源码学设计]蚂蚁金服SOFARegistry之存储结构 0x00 摘要 0x01 业务范畴 1.1 缓存 1.2 DataServ ...
- tengine下载和安装
tengine简介: Tengine所基于开发的Nginx的意思是Engine-X,Tengine在淘宝开发,所以我们把Engine-X中的X替换成Taobao.Tengine即Taobao-Engi ...
- 基数排序(Radix Sort)
基数排序(Radix Sort) 第一趟:个位 收集: 第二趟:十位 第三趟:百位 3元组 基数排序--不是基于"比较"的排序算法 递增就是把收集的过程返过来 算法效率分析 需要r ...
- JavaSE13-常用API&异常
1.包装类 1.1 基本类型包装类 基本类型包装类的作用 将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据 常用的操作之一:用于基本数据类型与字符串之间的转换 基本类型 包装 ...
- Java IO流 FileOutputStream、FileInputStream的用法
FileOutputStream.FileInputStream的使用 FileOutputStream是OutputStream的继承类,它的主要功能就是向磁盘上写文件.FileOutputStre ...
- react第三单元(react组件的生命周期)
第三单元(react组件的生命周期) #课程目标 灵活掌握react组件的生命周期以及组件的活动过程. 能够灵活使用react的生命周期 #知识点 react的类组件的生命周期分为三个阶段 实例期 存 ...