Description

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)的空间复杂度下计算出来,继续努力

最新文章

  1. Java面试:1
  2. Opera浏览器导出收藏到Chrome,和几个Chrome的一些小技巧
  3. [OC笔记] Category分类之见解
  4. 《深入理解Windows Phone 8.1 UI控件编程》基于最新的Runtime框架
  5. 关于Redis info的参数总结
  6. Name-based virtual servers 给予名称的虚拟服务
  7. WinForm 中两个窗口之间传递数据
  8. jdbc 连接Oracle informix Mysql
  9. 绝对炫的幻灯片插件-SKITTER
  10. [转载]android中The connection to adb is down,问题和解决
  11. OpenCV配置(Java)
  12. chapter 10 统计检验
  13. SmartGit 试用过期
  14. FZU 2168 防守阵地 I(前n项和的前n项和)
  15. JS中Node节点总结
  16. Javascript书籍推荐----(步步为赢)
  17. 学习 JavaScript (五)核心概念:语句
  18. [LeetCode] Rotate String 旋转字符串
  19. 【leetcode】893. Groups of Special-Equivalent Strings
  20. argunlar 1.0.0 【hello,world】

热门文章

  1. dataframe字段过长被截断
  2. java并发编程之美-阅读记录5
  3. 【原创】微信最新表情js代码
  4. php yield关键字以及协程的实现
  5. SSH工具
  6. [css]等高列的简单实现
  7. tensorflow基础重新巩固
  8. SQL语句计算经纬度距离
  9. iOS 109个Demo范例
  10. 微信小程序学习笔记(三)--框架-逻辑层