LeetCode:杨辉三角【118】

题目描述

给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。

在杨辉三角中,每个数是它左上方和右上方的数的和。

示例:

输入: 5
输出:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]

题目分析

  模拟杨辉三角的形成过程即可!

Java题解

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner; public class PascalsTriangle_118 {
public static List<List<Integer>> generate(int numRows) {
List<List<Integer>> ans = new ArrayList<>();
List<Integer> line = new ArrayList<>();
if(numRows<1)
return ans;
line.add(1);
ans.add(line);
if(numRows>1)
{
for(int i = 2;i<=numRows;i++)
{
line = new ArrayList<>();
line.add(1);
List<Integer> lastLine = ans.get(ans.size()-1);
for(int j = 1;j<i-1;j++)
{
line.add(lastLine.get(j)+lastLine.get(j-1));
}
line.add(1);
ans.add(line);
}
}
return ans;
} public static void main(String[] args) {
generate(5);
} }

  

LeetCode:杨辉三角【118】的更多相关文章

  1. leetcode 杨辉三角

    给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2,1], [ ...

  2. LeetCode 118. 杨辉三角

    118. 杨辉三角 给定一个非负整数numRows,生成杨辉三角的前numRows行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例 输入: 5 输出: [ [1], [1,1], [1,2 ...

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

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

  4. Leetcode#118. Pascal's Triangle(杨辉三角)

    题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...

  5. LeetCode 118:杨辉三角 II Pascal's Triangle II

    公众号:爱写bug(ID:icodebugs) 作者:爱写bug 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. Given a non-negative index k whe ...

  6. Java实现 LeetCode 118 杨辉三角

    118. 杨辉三角 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], ...

  7. LeetCode(118):杨辉三角

    Easy! 题目描述: 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1] ...

  8. leetcode 118. 杨辉三角(python)

    给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5输出:[ [1], [1,1], [1,2,1], [1, ...

  9. [leetcode]118,119PascalsTriangle,杨辉三角1,2

    杨辉三角1Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,R ...

随机推荐

  1. MySQL:cannot allocate the memory for the buffer pool

    InnoDB: The InnoDB memory heap is disabled InnoDB: Mutexes and rw_locks use GCC atomic builtins Inno ...

  2. jpeg错误打印结构

    struct jpeg_error_mgr { /* Error exit handler: does not return to caller */ JMETHOD(void, error_exit ...

  3. eclipse 建立maven项目 显示红叉的解决方法

    1.建立好之后就会发现项目有红叉. 这时发现查查在main处,打开项目>属性>Java Build Path>source,发现里边有红叉(如下图),这是由于我们的src/main下 ...

  4. atom安装插件

    1. 由于墙的原因,在界面中安装不了 先安装 npm 可以下载node.js桌面版命令行 2.打开命令行中进入atom的安装目录,进入到packages 中 3. 进入github 官网 输入你要下载 ...

  5. Spring3.2.6 + hibernate4.2.8 + hibernate-generic-dao1.2.0

    n多方法都不成功,最后在hibernate-generic-dao官网上的例子hibernate-maven-web(使用其jar包及配置,并将hibernate更新为4.2.8,加入ehcache及 ...

  6. 第一百七十三节,jQuery,Ajax

    jQuery,Ajax 学习要点: 1.Ajax 概述 2.load()方法 3.$.get()和$.post() 4.$.getScript()和$.getJSON() 5.$.ajax()方法 6 ...

  7. SSH框架解析

    当也许能够说是与你的初恋.妙龄的少女開始有些羞涩.是时候说说SHH的故事了.SHH是指Spring+Struts+Hibernate.还记研究生复试那天的面试,老师说了一句话Spring的核心是控制反 ...

  8. 转载: vim使用技巧

    两篇很牛的vim使用技巧   来源: ChinaUnix博客 日期: 2009.07.06 10:18 (共有条评论) 我要评论   读本文之前请注意:1. 本文的目标是提供一些vim的使用技巧,利用 ...

  9. 同步(Sync)/异步(Async),阻塞(Block)/非阻塞(Unblock)四种调用方式

    1. 概念理解        在进行网络编程时,我们常常见到同步(Sync)/异步(Async),阻塞(Block)/非阻塞(Unblock)四种调用方式:   同步/异步主要针对C端: 同步:    ...

  10. awk合并两个文件并显示

    问题: 答案: