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]
]
思路:动态规划,前面的构建出来了,后面根据某种关系也就可以构建出来。
这个要根据前一行来构建下一行。这里的for循环是直接遍历当前行的个数,往里面添加多少个。这里主要运用当前行的规律
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> result=new ArrayList<List<Integer>>();
if(numRows==0) return result;
List<Integer> row,pre=null;//row代表当前行,pre代表前一行
for(int i=1;i<=numRows;i++){//第几行,第几行就有几个元素
row=new ArrayList<>();
/*这里for循环中的步骤很巧妙,遍历当前行,若为开头或结尾,加1,其他情况再从前一行中计算,这样一开始就不会从前一行计算,所以不会出问题。
*/
for(int j=0;j<i;j++){//当前行的元素下标
if(j==0||j==i-1)
row.add(1);
else
row.add(pre.get(j-1)+pre.get(j));//一开始第一行第二行这里不会执行,
}
pre=row;
result.add(row);
} return result; }
还有一种更巧妙的,直接在一个list上操作。见代码。注意最后添加的时候new一个list,不然只能添加最终结果
class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> res=new ArrayList<List<Integer>>();
if(numRows==0) return res;
List<Integer> list=new LinkedList<>();
for(int i=1;i<=numRows;i++){
list.add(1);
for(int j=i-2;j>=1;j--){
list.set(j,list.get(j)+list.get(j-1));
}
//这里要新new一个list加进去,不然如果直接添加list,出现的情况是添加了最终结果(第一层for循环结束时的list)。
res.add(new LinkedList<Integer>(list));
}
return res;
}
}
Pascal's Triangle(杨辉三角)的更多相关文章
- [LeetCode] Pascal's Triangle 杨辉三角
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- [leetcode-118]Pascal's triangle 杨辉三角
Pascal's triangle (1过) Given numRows, generate the first numRows of Pascal's triangle. For example, ...
- LeetCode118. Pascal's Triangle 杨辉三角
题目 给定行数,生成对应的杨辉三角 思考 同一行是对称的,最大的下标为(行数+1)/2;1,1,2,3,6;下标从0开始,则对应分别为0.0.1.1.2.2 对于第偶数行,个数也是偶数,对于奇数行,个 ...
- 【LeetCode每天一题】Pascal's Triangle(杨辉三角)
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's t ...
- [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, ...
- 20190105-打印字母C,H,N,口等图像和杨辉三角
1. 打印字母C ****** * * * * ****** def print_c(n): print('*' * n) for i in range(n): print('* ') print(' ...
- [LeetCode] 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 118. Pascal's Triangle (杨辉三角)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- [Swift]LeetCode118. 杨辉三角 | Pascal's Triangle
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's t ...
随机推荐
- JEECG&Dubbo Demo
jeecg框架service与dao层类图 吴水成Dubbo Demo项目架构图
- Java-IO之BufferedOutputStream(缓冲输出流)
BufferedOutputStream是缓冲输出流,继承于FilterOutputStream,作用是为另外一个输出流提供换从功能. 主要函数列表: BufferedOutputStream(Out ...
- 1021. Deepest Root (25) -并查集判树 -BFS求深度
题目如下: A graph which is connected and acyclic can be considered a tree. The height of the tree depend ...
- jsoup详解
json相信大家都用的多,jsonp我就一直没有机会用到,但也经常看到,只知道是"用来跨域的",一直不知道具体是个什么东西.今天总算搞明白了.下面一步步来搞清楚jsonp是个什么玩 ...
- Linux IPC实践(9) --System V共享内存
共享内存API #include <sys/ipc.h> #include <sys/shm.h> int shmget(key_t key, size_t size, int ...
- (五十)Quartz2D生成图片的一些应用
应用一:给图片打水印,不应该是画到View的Layer上,而应该画到Bitmap上,产生一张新的图片. 1.首先读入背景图,然后开启一个位图上下文,并将它画在位图上下文上: UIImage *bgIm ...
- EBS中内部银行相关API
来自:http://www.itpub.net/thread-1772135-1-1.html 1.创建银行 -- Create Bank DECLARE p_init_msg_list VARCHA ...
- 【翻译】EXTJS 编码风格指南与实例
原文:EXTJS Code Style Guide with examples Ext JS风格指南: 熟知的且易于学习 快速开发,易于调试,轻松部署 组织良好.可扩展和可维护 Ext JS应用程序的 ...
- SMO实现
#include "stdio.h" #include <vector> using namespace std; float function(float alfa[ ...
- 【freeradius2.x】 安装和学习
虚拟机中centos 安装和学习 radius2 版本是2.2.x 的使用等知识 安装 为了测试方面,yum安装 yum -y install freeradius* 配置文件的位置是 /etc/ra ...