Description

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


In Pascal's triangle, each number is the sum of the two numbers directly above it.

Example:

Input:
Output:
[
[],
[,],
[,,],
[,,,],
[,,,,]
]

题目描述,给定一个数量,输出一个帕斯卡三角。

分析: 第一和第二的时候,为全1 后面的除了第一位和最后一位是1 其他的为上面的对应的相邻的两个数的和

C#代码如下:

public IList<IList<int>> Generate(int numRows)
{
List<IList<int>> result = new List<IList<int>>(numRows);
int index = ;
while(index < numRows)
{
IList<int> temp = new List<int>();
if (index == || index == )//第一行和第二行
{
for (int i = ; i <= index; i++)
temp.Add();
}
else
{
temp.Add();//在第一位加1
IList<int> preTemp = result[index - ];
for (int i =; i < preTemp.Count-; i++)//填充中间的数值
{
int val = preTemp[i] + preTemp[i+];
temp.Add(val);
}
temp.Add();//在最后一位加1
}
result.Add(temp);
index++;
}
return result;
}

LeetCode Array Easy 118. Pascal's Triangle的更多相关文章

  1. LeetCode Array Easy 119. Pascal's Triangle II

    Description Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's tria ...

  2. [LeetCode&Python] Problem 118. Pascal's Triangle

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

  3. 【leetcode❤python】118. Pascal's Triangle

    #-*- coding: UTF-8 -*-#杨辉三角class Solution(object):    def generate(self, numRows):        "&quo ...

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

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

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

  6. LN : leetcode 118 Pascal's Triangle

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

  7. 118. Pascal's Triangle

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

  8. LeetCode(119) Pascal's Triangle II

    题目 Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [ ...

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

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

随机推荐

  1. MYSQL中常用的工具

    1.mysql(客户端链接工具):   -u :指定用户名   -p:指定密码   -h:指定服务器ip或者域名   -P(大写):指定端口  例子:mysql -u root -h 202.194. ...

  2. k8s之ingress-nginx部署一直提示健康检查10254端口不通过问题就处理

    之前部署了一套k8s集群,但是到部署ingress-nginx的时候,一直提示10254端口拒绝不通:如下图. 这是因为我之前装的是docker1.17.默认的驱动是systemd.因为systemd ...

  3. ORM:Chloe

    ORM的一种:Chloe注意实体类模板特色:多表连接 利用chloe实现对各表的增删查改的管理,判断现有物料是否能够支持生产规模. 一开始报错: IQuery<ProductionPlans&g ...

  4. Linux面试基础(二)

    Linux常用目录——存放 /bin  所有用户可以使用的可执行文件 /sbin  新管理员使用的执行文件 /boot  Linux内核映像文件和与引导加载有关的文件 /dev   设备文件 /etc ...

  5. sublime text的快捷键

    Ctrl + Shift + P:调出命令板(Command Palette)Ctrl + `:调出控制台Ctrl + Enter:在当前行下面新增一行然后跳至该行Ctrl + Shift + Ent ...

  6. 一、创建且运行JPA工程

    1. 创建JPA 工程 (1)选择创建 JPA Project,注意不是Java Project (2)JPA version选择 2.0 (3)选择用户库,否则会出现 At least one us ...

  7. Linux中的系统服务_01

    Linux中的系统服务_01 1.linux中所有的内容都是以文件的形式存在,所以其服务也是以文件的形式存在/etc/init.d目中, 2.存在改/etc/init.d/目录里的文件,都可以通过命令 ...

  8. Hashtable、HashMap、TreeMap、ConcurrentHashMap、ConcurrentSkipListMap区别

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11444013.html 并发场景下的Map容器使用场景 如果对数据有强一致要求,则需使用Hashtab ...

  9. vue 路由动态传参 (多个)

    动态传参 传值页面  客户列表clientList.vue 路由 router.js 配置路由 接收参数的页面  客户详情CustomerDetails.vue 通过this.$router.para ...

  10. BlueStore-先进的用户态文件系统《二》-BlueFS

    https://zhuanlan.zhihu.com/p/46362124 简介 上一篇文章中,介绍了BlueStore的诞生背景.逻辑架构以及设计思想,提到了在BlueStore中元数据都是存放在R ...