LeetCode Array Easy 119. Pascal's Triangle II
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)的空间复杂度下计算出来,继续努力

LeetCode Array Easy 119. Pascal's Triangle II的更多相关文章
- LeetCode Array Easy 118. Pascal's Triangle
Description Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. I ...
- 【leetcode❤python】119. Pascal's Triangle II
#-*- coding: UTF-8 -*-#杨辉三角返回给定行#方法:自上而下考虑问题,从给定的一行计算出下一行,则给定行数之后,计算的最后一行就是求解的最后一行class Solution(obj ...
- 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- ...
- 【LeetCode】118 & 119 - Pascal's Triangle & Pascal's Triangle II
118 - Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, ...
- LeetCode OJ 119. Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
- 118/119. Pascal's Triangle/II
原文题目: 118. Pascal's Triangle 119. Pascal's Triangle II 读题: 杨辉三角问题 '''118''' class Solution(object): ...
- 119. Pascal's Triangle II@python
Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note t ...
- 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 [1,3, ...
- [LeetCode] 119. 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 ...
随机推荐
- 别人整理的dp题目
动态规划 动态规划 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 1322, 14 ...
- 解决MVC中textarea出现多余空格的问题
public static MvcHtmlString FixedTextAreaFor<TModel, TProperty>(this HtmlHelper<TModel> ...
- 【问题解决方案】本地仓库删除远程库后添加到已有github仓库时仓库地址找不到的问题(github仓库SSH地址)
参考: 我参考我自己.jpg 背景: 想添加一下远程库,github主页找了半天,Google搜索了半天,都没有找到,所以这里写一个,记录一下 1-格式分析:git@github.com:用户名/仓库 ...
- 看不懂源码?先来恶补一波Object原型吧
目录 Object Object属性 1.Object.prototype 2.Object.name Object方法 1.Object.assign() 2.Object.create() 3.O ...
- jQuery封装轮播图插件
// 布局要求,必须有一个容器,图片和两个按钮,布局方式自定,小圆点样式固定 // <div class="all"> // <img src="img ...
- 【转】java中JVM的原理
转载自https://blog.csdn.net/witsmakemen/article/details/28600127/ 一.java虚拟机的生命周期: Java虚拟机的生命周期 一个运行中的Ja ...
- 关于python全局变量
今天踩了一个坑,记录一下,避免后犯 在constant.py 中定义了一个全局变量 ZH_LIST= [],以个用于存储配置一些信息: 在views.py 中引用了这个ZH_LIST : 然后在app ...
- python读写excel(xlrd、xlwt)
一.读excel表 读excel用到xlrd模块,写excel用到xlwt模块: # 1.导入模块 import xlrd # 2.打开Excel文件读取数据 workbook = xlrd.open ...
- OpenCV常用基本处理函数(5)图像模糊
2D卷积操作 cv.filter2D() 可以让我们对一幅图像进行卷积操作, 图像模糊(图像平滑)使用低通滤波器可以达到图像模糊的目的.这对与去除噪音很有帮助.其实就是去除图像中的高频成分(比如:噪音 ...
- Update Vim to 8.0 in Ubuntu
add PPA sudo add-apt-repository ppa:jonathonf/vim Update and Install sudo apt-get update sudo apt-ge ...
