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 ...
随机推荐
- 容器下载的是centos8的镜像,scp出现packet_write_wait: Connection to **** port 22: Broken pipe 问题解决
解决方案:在~/.ssh目录新建文件config vi ~/.ssh/config #Added lines to fix. Host * IPQoS lowdelay t ...
- db2别名&同义词
创建别名: create alias alias_name for tab_name|view_name... 删除别名: drop alias alias_name 创建同义词(synonym): ...
- js 屏蔽网页快捷键代码
<script> function KeyDown(){ //屏蔽鼠标右键.Ctrl+n.shift+F10.F5刷新.退格键 //alert("ASCII代码是:"+ ...
- opensns建站
opensns建站 标签(空格分隔):软件工程 贪玩蓝月 购买云服务器 首选阿里云,一个月9.9元,还能直接安全宝塔Linux界面 后台初始化 输入ip地址:8888访问宝塔后台,第一次进入后台会让你 ...
- MQ总结
对于ActiveMQ:JMS可靠消息机制.JMS发布订阅与点对点通讯.ActiveMQ高可用集群解决方案.ActiveMQ补偿与重试策略.ActiveMQ整合SpringBoot Kafka:整合Zo ...
- 人生苦短_我用Python_list(列表)_002
# coding=utf-8 # 列表 list 列表函数 # 列表的特性: 可以有任何类型的值 可以更改 # 可以更改.增加.删除.修改 # 增加 append 直接在尾巴增加 list_1 = [ ...
- UNP学习第四章tcp
一.TCP简单流程图 因为对于server我已经写过一篇笔记了:http://www.cnblogs.com/ch122633/p/8315883.html 所以我想再补充一些对于client的部分的 ...
- 饿了么 <el-input></el-input>输入框获取与失去焦点事件
//1.定义focus事,绑定属性 <el-input v-model="headerInput" @focus="onInputFocus" @blur ...
- element-uI隐藏表格头部
1.表格结构定义 :show-header="hiddenTableHeader" 2. data里面定义 hiddenTableHeader:false,
- ANSI 标准C 还定义了如下几个宏
ANSI 标准C 还定义了如下几个宏:_LINE_ 表示正在编译的文件的行号_FILE_ 表示正在编译的文件的名字预处理名称意义#define 宏定义#undef 撤销已定义过的宏名#include ...
