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 ...
随机推荐
- Libgdx中TextButton的一些思考
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/caihongshijie6/article/details/37566183 由于有 ...
- Guava 开源工具的简单介绍
Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, primitives support, concurrency libra ...
- 回车\r的含义
package main import "fmt" func main() { // \r 回车,从当前行的最前面开始输出,覆盖掉以前的内容 // 输出:曹操刘备关羽 fmt.Pr ...
- JS同行绑定事件
<td><a class="blue" href="javascript:void(0);" class="blue" s ...
- 多个ip地址获取
#include "stdafx.h"#include <stdio.h> #include <winsock.h> #include <window ...
- MySQL日志文件与分析
1.查询日志.慢查询日志.二进制日志对比 查询日志 general_log 会记录用户的所有操作,其中包含增删查改等 可以指定输出为表 慢查询日志 slow_log 只要超过定义时间的所有操作语句都记 ...
- 你(可能)不知道的 web api
转自奇舞周刊 简介 作为前端er,我们的工作与web是分不开的,随着HTML5的日益壮大,浏览器自带的webapi也随着增多.本篇文章主要选取了几个有趣且有用的webapi进行介绍,分别介绍其用法.用 ...
- 浅谈maven自动化构建工具
转载https://blog.csdn.net/zxm1306192988/article/details/76209062 Maven是什么[what] 1.Maven 是 Apache 软件基金会 ...
- window.onload中失效的问题
在页面中,我们有时候想让页面加载的时候有多个JS事件,一般的时候我们会这样做 window.onload=function(){ alert("aaa"); } window.on ...
- 【leetcode】835. Image Overlap
题目如下: 解题思路:抛开移动的过程只看移动完成的结果,记图片左上角为顶点,正方形边长为board,要使得两个图片要有重叠,那么一定其中一张图片的顶点和另外一张图片的某一个点重合.假设图片A的顶点A( ...