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. MyBatis中XML 映射配置文件的简单介绍

    官网写的比较具体,可以查看以下的网站: http://www.mybatis.org/mybatis-3/zh/configuration.html 另外,实际用到标准的CRUD的操作和查询列表, & ...

  2. P3391 文艺平衡树(Splay)

    题目背景 这是一道经典的Splay模板题--文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...

  3. 【问题解决方案】anaconda-python在cmd-pip安装requests后依然提示No module named requests

    参考: 知乎回答:python的requests安装后idle依然提示No module named requests? 环境: win7-64位 anaconda3-Python3.7 & ...

  4. JS异步事件顺序:setTimeout,async,promise

    为什么最近更新那么频繁,还不是因为笔试的时候瞎了? 先说异步事件执行顺序的规则: 1. 定时器异步队列和promise队列不是同一队列,promise优先级高于setTimeout; 2. 创建pro ...

  5. LeetCode Array Easy 268. Missing Number

    Description Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one th ...

  6. linux ---pgbouncer的安装和配置

    pgbouncer是一款轻量级针对postgresql的数据库连接工具,可以对客户端的连接做限制,防止恶意连接,另外也可以减少数据库的实际连接数,从而减少数据库的开销. 环境: centos 6.5 ...

  7. Tomcat 配置错误界面

    Tomcat发生错误时跳转到错误页面 注意 :5.0下操作需要删除掉注释语句,不然报错,原因未知 一.修改 tomcat 的配置文件 修改 tomcat 的配置文件,当页面发生错误时跳转到指定的页面, ...

  8. Cesium截图功能

    首先安装  canvas2image npm intsall canvas2image --save 因为项目基于vue,所以需要在canvas2image的最后面 加上 export default ...

  9. 简单的51单片机多任务操作系统(C51)

    在网上看到这段代码,所以自己尝试了,可以跑起来,但是没有精确的定时功能,仅仅是任务的调度而已. 数组中是11,而不是12.这里写错了... /* 简单的多任务操作系统 其实只有个任务调度切换,把说它是 ...

  10. nginx启动报错: libpcre.so.1/libpcre.so.0: cannot open shared object file

    1.用file /bin/ls查看当前系统是几位的 2.64位系统创建软连接:ln -s /usr/local/lib/libpcre.so.1 /lib64 3.32未系统创建软连接:ln -s / ...