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. (十二)Bind读取配置到C#实例

    继续上一节的,接下来用Options或者Bind把json文件里的配置转成C#的实体,相互之间映射起来.首先新建一个asp.net core mvc项目OptionsBindSample Startu ...

  2. Python二分查找算法

    Python 二分查找算法: 什么是二分查找,二分查找的解释: 二分查找又叫折半查找,二分查找应该属于减值技术的应用,所谓减值法,就是将原问题分成若干个子问题后,利用了规模为n的原问题的解与较小规模( ...

  3. setlocale - 设置当前的区域选项

    总览 (SYNOPSIS) #include <locale.h> char *setlocale(int category, const char * locale); 描述 (DESC ...

  4. RAD介绍及实战,LVM介绍及实战,磁盘常见故障

    目录 一.RAID 1.RAID好处: 2.RAID的运行方式: 3.RAID的级别: 二.RAID实战 软RAID 1.RAID0 2.RAID1 3.RAID5 4.RAID10 三.LVM介绍 ...

  5. Es学习第四课, 倒排索引

    大家知道,ES的发明者初衷是想做一个搜索引擎给自己老婆用来搜菜谱,所以ES的核心工作就是做搜索,下面我们就开始讲关于搜索方面的知识点. DOC的概念我们第一课就讲过,它是ES存储数据的最小单元,我们再 ...

  6. typedef 函数指针的使用(含例子)

    C代码   //#include<iostream.h>   #include<stdio.h>      typedef int (*FP_CALC)(int, int);  ...

  7. jcrop+java 后台

    //jcrop 用法 lpf //页面引入 //<link rel="stylesheet" href="${basePath}/scripts/jcrop/jqu ...

  8. PHP filter_input_array() 函数

    定义和用法 filter_input_array() 函数从脚本外部获取多项输入(比如表单输入),并进行过滤. 该函数对过滤多个输入变量很有用,无需重复调用 filter_input(). 该函数可从 ...

  9. linux系统一些信息(待整理)

    Linux查看物理CPU个数.核数.逻辑CPU个数 复制代码 # 总核数 = 物理CPU个数 X 每颗物理CPU的核数 # 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 X 超线程数 ...

  10. Django的流程如何理解(餐厅点餐举例)

    去饭店(商场)吃饭的步骤: 告诉前台服务员,来一小碗牛肉拉面,菜单上勾上一个牛肉拉面(url) 服务员去拉面窗口,告诉后厨,一碗牛肉拉面),后厨(view)开始准备. 后厨给打杂小弟说,给我一份儿面条 ...