LeetCode Array Easy 119. Pascal's Triangle II
2024-10-07 18:13:46
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.Example:
Input:
Output: [,,,]Follow up:
Could you optimize your algorithm to use only O(k) extra space?
题目描述,给定一个行的索引值,返回当前行的帕斯卡三角当前行的值。
进阶:在O(k)的空间复杂度上完成
思路:在思考在O(k)的空间复杂度上实现未果,转而使用普通实现。用两个数组来实现,一个数组prev负责保存保存上一行的数,用来计算下一行的值,最终计算出要求的行的数。
C#解法:
public IList<int> GetRow(int rowIndex) {
int[] prev = null;
List<int> result = new List<int>();
if (rowIndex == )
result.Add();
else
{ result.AddRange(new int[] { , });
while (rowIndex > )
{
prev = result.ToArray(); result.Clear();
result.Add();
for (int i = ; i < prev.Length - ; i++)
{
int val = prev[i] + prev[i + ];
result.Add(val);
}
result.Add();
rowIndex--;
} }
return result;
}
还是没能在O(k)的空间复杂度下计算出来,继续努力
最新文章
- Java面试:1
- Opera浏览器导出收藏到Chrome,和几个Chrome的一些小技巧
- [OC笔记] Category分类之见解
- 《深入理解Windows Phone 8.1 UI控件编程》基于最新的Runtime框架
- 关于Redis info的参数总结
- Name-based virtual servers 给予名称的虚拟服务
- WinForm 中两个窗口之间传递数据
- jdbc 连接Oracle informix Mysql
- 绝对炫的幻灯片插件-SKITTER
- [转载]android中The connection to adb is down,问题和解决
- OpenCV配置(Java)
- chapter 10 统计检验
- SmartGit 试用过期
- FZU 2168 防守阵地 I(前n项和的前n项和)
- JS中Node节点总结
- Javascript书籍推荐----(步步为赢)
- 学习 JavaScript (五)核心概念:语句
- [LeetCode] Rotate String 旋转字符串
- 【leetcode】893. Groups of Special-Equivalent Strings
- argunlar 1.0.0 【hello,world】