C#LeetCode刷题之#118-杨辉三角(Pascal‘s Triangle)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3688 访问。
给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。
在杨辉三角中,每个数是它左上方和右上方的数的和。
输入: 5
输出:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
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.
Input: 5
Output:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3688 访问。
public class Program {
public static void Main(string[] args) {
var res = Generate(5);
ShowArray(res);
Console.ReadKey();
}
private static void ShowArray(IList<IList<int>> array) {
foreach(var num in array) {
foreach(var num2 in num) {
Console.Write($"{num2} ");
}
Console.WriteLine();
}
Console.WriteLine();
}
private static IList<IList<int>> Generate(int numRows) {
if(numRows == 0) {
return new int[][] { };
}
int[][] res = new int[numRows][];
for(int i = 0; i < res.Length; i++) {
res[i] = new int[i + 1];
}
res[0][0] = 1;
for(int i = 1; i < numRows; i++) {
res[i][0] = 1;
for(int j = 1; j < i + 1; j++) {
if(j >= i) {
res[i][j] = res[i - 1][j - 1];
} else {
res[i][j] = res[i - 1][j - 1] + res[i - 1][j];
}
}
}
return res;
}
}
以上给出1种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3688 访问。
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
分析:
显而易见,以上参考算法在最坏的情况下的时间复杂度为: ,空间复杂度也为:
。
C#LeetCode刷题之#118-杨辉三角(Pascal‘s Triangle)的更多相关文章
- [Swift]LeetCode118. 杨辉三角 | Pascal's Triangle
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's t ...
- LeetCode 118. 杨辉三角
118. 杨辉三角 给定一个非负整数numRows,生成杨辉三角的前numRows行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例 输入: 5 输出: [ [1], [1,1], [1,2 ...
- Java实现 LeetCode 118 杨辉三角
118. 杨辉三角 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], ...
- leetcode 118. 杨辉三角(python)
给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5输出:[ [1], [1,1], [1,2,1], [1, ...
- LeetCode(119):杨辉三角 II
Easy! 题目描述: 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 3 输出: [1,3,3,1] 进阶: ...
- C语言118. 杨辉三角
给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5输出:[ [1], [1,1], [1,2,1], [1, ...
- 杨辉三角(Pascal Triangle)的几种C语言实现及其复杂度分析
说明 本文给出杨辉三角的几种C语言实现,并简要分析典型方法的复杂度. 本文假定读者具备二项式定理.排列组合.求和等方面的数学知识. 一 基本概念 杨辉三角,又称贾宪三角.帕斯卡三角,是二项式系数在三 ...
- Leecode刷题之旅-C语言/python-118杨辉三角
/* * @lc app=leetcode.cn id=118 lang=c * * [118] 杨辉三角 * * https://leetcode-cn.com/problems/pascals-t ...
- C#LeetCode刷题-数组
数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...
- leetcode刷题目录
leetcode刷题目录 1. 两数之和 2. 两数相加 3. 无重复字符的最长子串 4. 寻找两个有序数组的中位数 5. 最长回文子串 6. Z 字形变换 7. 整数反转 8. 字符串转换整数 (a ...
随机推荐
- 【一起学系列】之命令模式:封装一个简单Jedis
意图 将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化:对请求排队或记录请求日志,以及支持可撤销的操作. 命令模式的诞生 [产品]:开发小哥,来活啦,咱们需要设计一款遥控器,核心功能就 ...
- 洛谷P1063.能量项链
题目描述 在Mars星球上,每个Mars人都随身佩带着一串能量项链.在项链上有N颗能量珠.能量珠是一颗有头标记与尾标记的珠子,这些标记对应着某个正整数.并且,对于相邻的两颗珠子,前一颗珠子的尾标记一定 ...
- Android 性能优化---布局优化
Android 性能优化---布局优化 Android 布局绘制原理 布局加载过程 setContentView() --> inflate() -- > getLayout()(I/O操 ...
- python-study-文件操作
# 一.文件操作的作用 :读取内容.写入内容.备份内容.... # 文件的基本操作,文件操作包含:打开.关闭.读.写.复制.... # 打开 读写 关闭 # 文件备份 # 文件和文件夹的操作 # 总结 ...
- linux root 与普通用户之间的切换
test@ubuntu:~$ su Password: root@ubuntu:/home/uu# 也可以是从root用户切换到普通用户.如果当前是root用户,那么切换成普通用户uu用以下命令:s ...
- PHP trigger_error() 函数
定义和用法 trigger_error() 函数创建用户自定义的错误消息. trigger_error() 函数用于在用户指定的条件下触发一个错误消息.它可以与内建的错误处理程序一起使用,或者与由 s ...
- PHP defined() 函数
实例 检查某常量是否存在: <?phpdefine("GREETING","Hello you! How are you today?");echo de ...
- 死磕HashMap
前言 HashMap是Java中最常用的集合类框架,也是Java语言中非常典型的数据结构,同时也是我们需要掌握的数据结构,更重要的是进大厂面试必问之一. 数组特点 存储区间是连续,且占用内存严重,空间 ...
- [C#] (原创)一步一步教你自定义控件——01,TrackBar
一.前言 技术没有先进落后之分,只有合不合适. WinForm有着非常多的优点,在使用WinForm久了之后,难免会觉得WinForm自带的某些控件外观上有些许朴素.或者功能上有些不如意,自然而然便想 ...
- MIME-TYPE 列表
Suffixes applicable Media type and subtype(s) .3dm x-world/x-3dmf .3dmf x-world/x-3dmf .a applicatio ...