[Leetcode][JAVA] Pascal's Triangle I, II
Pascal's Triangle:
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]
] 已知行数生成帕斯卡三角。实际上只要有第i层,那么就能生成第i+1层。每次新生成的层加入最终集合中即可。
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> re = new ArrayList<List<Integer>>();
for(int i=0;i<numRows;i++) {
List<Integer> temp = new ArrayList<Integer>();
for(int j=0;j<=i;j++) {
if(j==0 || j==i)
temp.add(1);
else
temp.add(re.get(i-1).get(j-1)+re.get(i-1).get(j));
}
re.add(temp);
}
return re;
}
Pascal's Triangle II:
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?
与第一题几乎一样,只不过不需要返回整个三角而只需要返回最后一层。全程只需要维护生成层和它上一层两个ArrayList即可。
public List<Integer> getRow(int rowIndex) {
List<Integer> re = new ArrayList<Integer>();
for(int i=0;i<=rowIndex;i++) {
List<Integer> temp = new ArrayList<Integer>();
for(int j=0;j<=i;j++) {
if(j==0 || j==i)
temp.add(1);
else
temp.add(re.get(j-1) + re.get(j));
}
re = temp;
}
return re;
}
[Leetcode][JAVA] Pascal's Triangle I, II的更多相关文章
- 【leetcode】Pascal's Triangle I & II (middle)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- [LeetCode] 119. Pascal's Triangle II 杨辉三角 II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
- LeetCode 118 Pascal's Triangle
Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- Java for LeetCode 119 Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- leetcode 119 Pascal's Triangle II ----- java
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
- Java [Leetcode 119]Pascal's Triangle II
题目描述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return ...
- LeetCode:Pascal's Triangle I II
LeetCode:Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For examp ...
- LeetCode 119. Pascal's Triangle II (杨辉三角之二)
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
随机推荐
- java学习第四天
那些逻辑语言就基本了解下,今天想到了一个问题就是关于for和while的区别,从专业上来说,for和while基本上是相同的,但是for是只允许一次访问的,如果结束后就无法继续访问,而while则可以 ...
- Inside The C++ Object Model - 03
object Lessons 1.C++中布局以及存取时间上的的额外负担是由virtual引起的:virtual function.virtual base class.或是由于多继承引起的. 2.C ...
- 指针的指针&指向指针数组的指针
一.指针的指针 指针的指针看上去有些令人费解.它们的声明有两个星号.例如: char ** cp; 如果有三个星号,那就是指针的指针的指针,四个星号就是指针的指针的指针的指针 ...
- NRF51822之修改设备名(掉电不保存)
主要代码 /**@brief Function for handling the Application's BLE Stack events. * * @param[in] p_ble_evt Bl ...
- [Tomcat 源码分析系列] (附件) : catalina.bat 脚本
摘自 apache-tomcat-8.0.39-src 源码包中的 catalina.bat 脚本内容 @echo off rem Licensed to the Apache Software Fo ...
- notepad++ 右键
在网上搜索建立reg 后运行, 虽然右键菜单出现了建立的右键项目名,但与软件不关联 Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\*\ ...
- codevs4919 线段树练习4
4919 线段树练习4 时间限制: 1 s 空间限制: 128000 KB 题目描述 Description 给你N个数,有两种操作 1:给区间[a,b]内的所有数都增加X 2:询问区 ...
- 深入理解js——一切都是对象
"一切皆对象" 当然也不是所有的都是对象,值类型(undefined,number,string,boolean)就不是对象:而函数.对象.数组.null.new Number(1 ...
- Notepad++ HTML格式化
[Notepad++ HTML格式化] Tidy2.
- 慕课网JavaScript入门篇课程笔记
1.js注释很重要 单行注释,在注释内容前加符号 “//”. <script type="text/javascript"> document.write(" ...