Leetcode#118. Pascal's Triangle(杨辉三角)
题目描述
给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。

在杨辉三角中,每个数是它左上方和右上方的数的和。
示例:
输入: 5
输出:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
思路
对任意的n>0有
f(1, n)=1,(n>0)
f(1, 2)=1,(n=2)
f(i,j) = f(i-1, j-1)+f(i, j-1),i>2,j>2
代码实现
package Array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* 118. Pascal's Triangle(杨辉三角)
* 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。
*/
public class Solution118 {
public static void main(String[] args) {
Solution118 solution118 = new Solution118();
int numRows = 1;
List<List<Integer>> res = solution118.generate(numRows);
System.out.println(res);
}
/**
* 对任意的n>0有
* f(1, n)=1,(n>0)
* f(1, 2)=1,(n=2)
* f(i,j) = f(i-1, j-1)+f(i, j-1),i>2,j>2
*
* @param numRows
* @return
*/
public List<List<Integer>> generate(int numRows) {
if (numRows == 0) {
return Collections.emptyList();
}
List<List<Integer>> res = new ArrayList<>();
for (int i = 0; i < numRows; i++) {
List<Integer> sub = new ArrayList<>();
for (int j = 0; j <= i; j++) {
if (j == 0 || j == i) {
sub.add(1);
} else {
List<Integer> upSub = res.get(i - 1);
sub.add(upSub.get(j - 1) + upSub.get(j));
}
}
res.add(sub);
}
return res;
}
}
Leetcode#118. 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 ...
- Pascal's Triangle(杨辉三角)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- 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
Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- LN : leetcode 118 Pascal's Triangle
lc 118 Pascal's Triangle 118 Pascal's Triangle Given numRows, generate the first numRows of Pascal's ...
- leetcode 118. Pascal's Triangle 、119. Pascal's Triangle II 、120. Triangle
118. Pascal's Triangle 第一种解法:比较麻烦 https://leetcode.com/problems/pascals-triangle/discuss/166279/cpp- ...
随机推荐
- axios 使用
<!DOCTYPE html> <html lang="en"> <head> {#导入静态文件#} {% load static %} < ...
- 第二十九节,目标检测算法之R-CNN算法详解
Girshick, Ross, et al. “Rich feature hierarchies for accurate object detection and semantic segmenta ...
- python 装饰器的应用
import time def test1(): print "hello\n" print test1.__name__ def test2(): print "hel ...
- file 文件的操作
1.写入文件: (1)第一种方式 f = open("filename",'mode') #先打开一个文件,没有的话创建这个文件,mode是模式.有r 只读,w写,rw读写 ...
- 安装SVN并使用IDEA检出项目
首先去下载小王八:https://tortoisesvn.net/downloads.html 下载完毕,打开 .. ..注意勾选command line工具 .. .. 下一步,打开IDEA,配置S ...
- 开发问题及解决--java.lang.IllegalStateException: Circular dependencies cannot exist in RelativeLayout
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android=" ...
- makefile解析:一些常用函数
#======================================================================= #指定目标文件名,makefile中的变量直接使用不用 ...
- collections 数据结构模块namedtuple
namedtuple类 导入模块 from collections import namedtuple 使用方法及说明 #pycharm 里按住 ctrl键点击 collections可查看源码 #c ...
- vue-cli脚手架笔记
vue不支持IE8,因为vue使用了IE8无法模拟的 ECMAScript 5特性 使用vue-cli脚手架会让我们的工作非常方便 比如想编译一下es6的语法就使用 babel loader 和 ba ...
- Android GreenDao使用教程
1.在build.gradle里添加相关依赖 apply plugin: 'org.greenrobot.greendao' buildscript { repositories { mavenCen ...