题目描述

给定一个非负整数 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(杨辉三角)的更多相关文章

  1. [LeetCode] Pascal's Triangle 杨辉三角

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  2. [leetcode-118]Pascal's triangle 杨辉三角

    Pascal's triangle (1过) Given numRows, generate the first numRows of Pascal's triangle. For example, ...

  3. LeetCode118. Pascal's Triangle 杨辉三角

    题目 给定行数,生成对应的杨辉三角 思考 同一行是对称的,最大的下标为(行数+1)/2;1,1,2,3,6;下标从0开始,则对应分别为0.0.1.1.2.2 对于第偶数行,个数也是偶数,对于奇数行,个 ...

  4. 【LeetCode每天一题】Pascal's Triangle(杨辉三角)

    Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's t ...

  5. Pascal's Triangle(杨辉三角)

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  6. LeetCode 118. Pascal's Triangle (杨辉三角)

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  7. LeetCode 118 Pascal's Triangle

    Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows  ...

  8. LN : leetcode 118 Pascal's Triangle

    lc 118 Pascal's Triangle 118 Pascal's Triangle Given numRows, generate the first numRows of Pascal's ...

  9. 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- ...

随机推荐

  1. pthread_create()

    pthread_create()创建的线程没有执行,试了好几遍.非常郁闷.后来发现主线程没有睡眠,新创建的线程还没来的及执行主线程就退出了,所以没有执行.

  2. struts2 防止表单的重复提交

    防止表单重复提交(拦截器) 1.回顾之前的解决办法: 2.Struts2中的解决办法: 2.1.使用重定向 <result type="redirect">/succe ...

  3. 插入排序Java版

    package dataStructureAlgorithmReview.day01; import java.util.Arrays; /** * 插入排序 * @author shundong * ...

  4. NoSQL数据库Mongodb副本集架构(Replica Set)高可用部署

    NoSQL数据库Mongodb副本集架构(Replica Set)高可用部署 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. MongoDB 是一个基于分布式文件存储的数据库.由 C ...

  5. CentOS上手工部署kubernetes集群

    本文完全是根据二进制部署kubernets集群的所有步骤,同时开启了集群的TLS安全认证. 环境说明 在下面的步骤中,我们将在三台CentOS系统的物理机上部署具有三个节点的kubernetes1.7 ...

  6. 删除mysql数据库中表分区数据

    删除mysql数据库中表分区数据 zabbix 几个大表创建了分区,由于磁盘空间告警,特将3月前的分区给予删除. 1.查看表的数据占用磁盘空间情况 2.登录mysql中,查看表的分区情况. 3.删除表 ...

  7. Linux记录-CPU指标介绍

    在linux的系统维护中,可能需要经常查看cpu使用率,分析系统整体的运行情况.而监控CPU的性能一般包括以下3点:运行队列.CPU使用率和上下文切换. 对于每一个CPU来说运行队列最好不要超过3,例 ...

  8. Web API中的内容协商

    一.内容协商的概念 HTTP规范将内容协商定义为“当有多个格式可用时为给定响应选择最佳格式的过程”.HTTP中内容协商的主要机制是这些请求标头: Accept:响应可接受哪些媒体类型,例如“appli ...

  9. JVM 虚拟机笔记

    GC算法 标记-清除算法复制算法标记-整理算法分代收集 新生代--[15次GC]-->老年代新生代:复制算法老年代: 标记-清除算法或标记-整理算法 GC策略采用的何种算法引用计数算法根搜索算法 ...

  10. cmd命令查看当前IIS运行的网站iisapp appcmd

    xp,2003中用的命令是:iisapp -a 2007,2008,2012系统中: cmd命令行进入%windir%\system32\inetsrv\目录下 运行appcmd.exe list w ...