LeetCode Array Easy 118. Pascal's Triangle
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的更多相关文章
- 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 ...
- [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 ...
- 【leetcode❤python】118. Pascal's Triangle
#-*- coding: UTF-8 -*-#杨辉三角class Solution(object): def generate(self, numRows): "&quo ...
- Leetcode#118. Pascal's Triangle(杨辉三角)
题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...
- 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- ...
- LN : leetcode 118 Pascal's Triangle
lc 118 Pascal's Triangle 118 Pascal's Triangle Given numRows, generate the first numRows of Pascal's ...
- 118. Pascal's Triangle
题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...
- 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 [ ...
- LeetCode 118. Pascal's Triangle (杨辉三角)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
随机推荐
- java虚拟机规范(se8)——class文件格式(三)
4.5 字段 字段使用field_info结构来描述. 在同一个class文件中的两个字段不能有相同的名称和描述符. 结构的格式如下: field_info { u2 access_flags; u2 ...
- 算法8-5:Prim算法
Prim算法用于计算最小生成树.Prim算法分为两种,一种是懒汉式,一种是饿汉式. 懒汉式Prim 懒汉式Prim算法过程例如以下: 首先将顶点0增加到MST中 从MST与未訪问顶点之间边中选出最短的 ...
- 【转】Pandas速查手册中文版
本文翻译自文章:Pandas Cheat Sheet - Python for Data Science,同时添加了部分注解. 对于数据科学家,无论是数据分析还是数据挖掘来说,Pandas是一个非常重 ...
- SpringMVC学习(2):经典的HelloWorld实现
前一篇简单介绍了Spring MVC的一些知识,下面就要开始学习如何把Spring MVC运用到具体的项目中去. 首先还是从一个简单的Hello World项目说起: 我机器的开发环境为: Ubunt ...
- seaweedfs使用记录
搭建seaweedfs 在github上面clone,然后cd到docker目录使用docker-compose up -d就可以启动seaweedfs 启动以后通过xxx:9333可以看到效果 上传 ...
- erlang应用程序启动
(1)erlang应用程序启动过程中,还可以分阶段启动. 在erlang应用程序的资源文件*.app可以定义分步骤启动. *.app中的start_phase字 ...
- 0xC0000005: 写入位置 0x00000000 时发生访问冲突的解决办法(转)
上面的意识就是你吧值付给了不该赋给的变量,或者说你把值付给了不能付给的变量(或者常量) ()最简单也最直接的错误可能就是scanf()的问题,我们都知道输入的时候都是scanf("%格式&q ...
- k8s--网络模式
1.clusterip kind: Service apiVersion: v1 metadata: name: my-service spec: selector: app: nginx ports ...
- 【和孩子一起学编程】 python笔记--第二天
第六章 GUI:用户图形界面(graphical user interface) 安装easygui:打开cmd命令窗口,输入:pip install easygui 利用msgbox()函数创建一个 ...
- Python--模块之sys模块、logging模块、序列化json模块、序列化pickle模块
sys模块 sys.argv 命令行参数List,第一个元素是程序本身路径 sys.exit(n) 退出程序,正常退出时exit() sys.path 返回模块的搜索路径,初始化时使用PYTHONPA ...