问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3690 访问。

给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 行。

在杨辉三角中,每个数是它左上方和右上方的数的和。

输入: 3

输出: [1,3,3,1]

你可以优化你的算法到 O(k) 空间复杂度吗?


Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle.

Note that the row index starts from 0.



In Pascal's triangle, each number is the sum of the two numbers directly above it.

Could you optimize your algorithm to use only O(k) extra space?

Input: 3

Output: [1,3,3,1]


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3690 访问。

public class Program {

    public static void Main(string[] args) {
var res = GetRow(4);
var res2 = GetRow2(5); ShowArray(res);
ShowArray(res2); Console.ReadKey();
} private static void ShowArray(IList<int> array) {
foreach(var num in array) {
Console.Write($"{num} ");
}
Console.WriteLine();
} private static IList<int> GetRow(int rowIndex) {
int[][] res = new int[rowIndex + 1][];
for(int i = 0; i < res.Length; i++) {
res[i] = new int[rowIndex + 1];
}
res[0][0] = 1;
for(int i = 1; i < rowIndex + 1; i++) {
res[i][0] = 1;
for(int j = 1; j < i + 1; j++) {
res[i][j] = res[i - 1][j - 1] + res[i - 1][j];
}
}
return res[rowIndex];
} private static IList<int> GetRow2(int rowIndex) {
int[] res = new int[rowIndex + 1];
res[0] = 1;
for(int i = 1; i < rowIndex + 1; i++) {
for(int j = rowIndex; j >= 1; j--) {
res[j] = res[j - 1] + res[j];
}
}
return res;
} }

以上给出2种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3690 访问。

1 4 6 4 1
1 5 10 10 5 1

分析:

显而易见,GetRow在最坏的情况下的时间复杂度为:  ,空间复杂度也为: ;

GetRow2在最坏的情况下的时间复杂度为:  ,由于使用一维数组空间复杂度为:  。

GetRow2方法可以根据杨辉三角的对称性优化,只需计算一半即可,其实现方法留给各位看官。

C#LeetCode刷题之#119-杨辉三角 II(Pascal‘s Triangle II)的更多相关文章

  1. 算法:杨辉三角(Pascal's Triangle)

    一.杨辉三角介绍 杨辉三角形,又称帕斯卡三角形.贾宪三角形.海亚姆三角形.巴斯卡三角形,是二项式系数的一种写法,形似三角形,在中国首现于南宋杨辉的<详解九章算法>得名,书中杨辉说明是引自贾 ...

  2. Java实现 LeetCode 119 杨辉三角 II

    119. 杨辉三角 II 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 3 输出: [1,3,3,1] 进阶: ...

  3. 【LeetCode】119. 杨辉三角 II Pascal‘s Triangle II(Python & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 方法一: 空间复杂度 O ( k ∗ ( k + 1 ...

  4. LeetCode(119. 杨辉三角 II)

    问题描述 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 3 输出: [1,3,3,1] 进阶: 你可以优化你的 ...

  5. C#LeetCode刷题之#118-杨辉三角(Pascal‘s Triangle)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3688 访问. 给定一个非负整数 numRows,生成杨辉三角的前 ...

  6. 力扣119.杨辉三角II-C语言实现

    题目 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 3 输出: [1,3,3,1] 来源:力扣(LeetCod ...

  7. LeetCode(118):杨辉三角

    Easy! 题目描述: 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1] ...

  8. LeetCode 118:杨辉三角 II Pascal's Triangle II

    公众号:爱写bug(ID:icodebugs) 作者:爱写bug 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. Given a non-negative index k whe ...

  9. [LeetCode 119] - 杨辉三角形II(Pascal's Triangle II)

    问题 给出一个索引k,返回杨辉三角形的第k行. 例如,给出k = 3,返回[1, 3, 3, 1] 注意: 你可以优化你的算法使之只使用O(k)的额外空间吗? 初始思路 首先来复习复习杨辉三角形的性质 ...

  10. [Swift]LeetCode119. 杨辉三角 II | Pascal's Triangle II

    Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note t ...

随机推荐

  1. tcpreplay的使用指导

    Tcpreplay的介绍 简单的说, tcpreplay是一种pcap包的重放工具, 它可以将用ethreal, wireshark工具抓下来的包原样或经过任意修改后重放回去. 它允许你对报文做任意的 ...

  2. J.U.C体系进阶(一):juc-executors 执行器框架

    Java - J.U.C体系进阶 作者:Kerwin 邮箱:806857264@qq.com 说到做到,就是我的忍道! 主要内容: juc-executors 执行器框架 juc-locks 锁框架 ...

  3. Ethical Hacking - NETWORK PENETRATION TESTING(9)

    WEP Cracking Packet Injection What if the AP was idle, or had no clients associated with it? In this ...

  4. 老司机带你玩转面试(5):Redis 集群模式 Redis Cluster

    前文回顾 建议前面文章没看过的同学先看下前面的文章: 「老司机带你玩转面试(1):缓存中间件 Redis 基础知识以及数据持久化」 「老司机带你玩转面试(2):Redis 过期策略以及缓存雪崩.击穿. ...

  5. 太实用了!自己动手写软件——我们的密码PJ器终于完成了

    之前我们完成了密码破解工具的界面,今天我们来看看功能实现吧. 目录 编码 提交——功能实现 开始破解——功能实现 读取密码字典 选择协议并执行破解动作 POP3协议的破解函数 IMAP协议的破解函数 ...

  6. centos 构建dns服务 dnsmasq

    1 安装yum -y install dnsmasq开放udp tcp 53 端口2,修改配置文件 dnsmasq.conf# grep -Ev "^$|^[#;]" /etc/d ...

  7. C++语法小记---继承中的构造和析构顺序

    继承中构造和析构的顺序 先父母,后客人,最后自己 静态变量和全局变量在最开始 析构和构造的顺序完全相反 #include <iostream> #include <string> ...

  8. 【最短路+bfs+缩点】Paint the Grid Reloaded ZOJ - 3781

    题目: Leo has a grid with N rows and M columns. All cells are painted with either black or white initi ...

  9. 2Ants(独立,一个个判,弹性碰撞,想象)

    AntsDescriptionAn army of ants walk on a horizontal pole of length l cm, each with a constant speed ...

  10. python关于字符编码的基本操作

    字符编码 (注意:关于字符编码,如果没有特殊业务要求,请牢记仅使用UTF-8编码) 由于Python的字符串类型是str,在内存中以Unicode表示,一个字符对应若干个字节.如果要在网络上传输,或者 ...