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 ...
随机推荐
- 重读ORB_SLAM之LocalMapping线程难点
1. 认清几个锁与布尔参数 线程的通信与相互影响在ORB比较复杂,需要好好缕清思路. 1.1 mbStopRequested,由RequestStop函数设定,主要是在回环线程里,在运行全局优化时,以 ...
- Centos安装IDEA
1.官网下载tar包 到https://www.jetbrains.com/idea 下载对应版本的文件 将其解压 tar zvxf idea下载文件.tar 进入到解压后文件夹的bin目录下执行 . ...
- 三、Centos7安装Mysql
1.到服务器下载的链接 wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm 2.执行命令 sudo r ...
- Python3.5-20190507-廖老师-自我笔记-迭代
可以使用for x in 数据 的那么 这个数据就是可迭代对象. 通过计算生成下一个值的数据就是生成器 可以使用next(数据) 来计算出下一个值的数据就是迭代器(生成器属于迭代器) -------- ...
- ECUST_Algorithm_2019_3
简要题意及解析 1001 给出一个\(n\times m\)连连看的局面,下面有\(q\)次询问:两个位置的块能否消去,即两个位置的连线是否能少于两次转折,回答\(YES/NO\).与一般的连连看不同 ...
- java--ArrayList,LinkedList应用比较
import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class ListDem ...
- 浅谈CICD持续集成、持续部署的流程(转)
Jenkins是一个比较流行的持续集成工具GitLab是存储镜像的镜像仓库由客户端将代码push推送到git仓库,gitlab上配置了一个webHook的东西可以触发Jenkins的构建.进入到Jen ...
- 【leetcode】927. Three Equal Parts
题目如下: Given an array A of 0s and 1s, divide the array into 3 non-empty parts such that all of these ...
- 记一次pycharm和vscode因网络问题插件下载失败的问题
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connec ...
- 2019牛客第八场多校 E_Explorer 可撤销并查集(栈)+线段树
目录 题意: 分析: @(2019牛客暑期多校训练营(第八场)E_Explorer) 题意: 链接 题目类似:CF366D,Gym101652T 本题给你\(n(100000)\)个点\(m(1000 ...
