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. 75. Find Peak Element 【medium】

    75. Find Peak Element [medium] There is an integer array which has the following features: The numbe ...

  2. CentOS设置sendmail发件人,让sendmail不显示通过XXX代发

    0.有一个十分快速的方法 命令:hostname itzhanzhang.com,但是重启后会失效,于是请接着往下看一劳永逸的方法: 1.设置你的主机名 默认的主机名是类似于“VM_24_76_cen ...

  3. 43. Spring Boot动态数据源(多数据源自动切换)【从零开始学Spring Boot】

    [视频&交流平台] àSpringBoot视频 http://study.163.com/course/introduction.htm?courseId=1004329008&utm ...

  4. 设置一个label显示多种颜色,多种字体大小

    UILabel* label = [[UILabel alloc] init]; label.frame = CGRectMake(0, 100, 200, 100); label.textColor ...

  5. Shiro学习(总结)

    声明:本文原文地址:http://www.iteye.com/blogs/subjects/shiro 感谢开涛提供的博文,让我学到了非常多.在这里由衷的感谢你,同一时候我强烈的推荐开涛的博文.他的博 ...

  6. 做前端(单纯页面和js)遇到的问题辑录(一)

    html标签的name和id的值一样,jQuery在选择的时候会混乱么? 1.超链接<a href="http://www.jb51.net" title="脚本之 ...

  7. FreeBSD这就是你的速度???

    想一心一意的用一款UNIX操作系统,真不容易,FreeBSD你为啥这么坑? 用pkg安装软件,国内没有相应的源,只好从官网上下,欲哭无泪!有点怀恋CentOS的yum了,163的源,那速度杠杠滴! 先 ...

  8. 设计模式中类的关系之聚合关系(Aggregation)

    聚合关系是关联关系的一种特例,它体现的是整体与部分的关系,即has-a的关系,此时整体与部分之间是可分离的,它们可以具有各自的生命周期,部分可以属于多个整体对象,也可以为多个整体对象共享.比如计算机与 ...

  9. Vsphere笔记06 Vcenter 部署流程 1

    Vcenter 部署流程 1   一.环境需求   1.需要两台装着WIN2K8 R2 64X的服务器   2.启用一台要添加活动目录角色,并且配置DC,DC的参数如下: 域名:justech-dc. ...

  10. php 远程调用redis

    <?php $redis_conf = array ( "active_code"=>array( "host" => "14.29 ...