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 ...
随机推荐
- automapper实体中的映射和聚合根中的使用
一,如下例子: using AutoMapper; using System; using System.Collections.Generic; using System.Linq; using S ...
- SET TRANSACTION - 设置当前事务的特性
SYNOPSIS SET TRANSACTION [ ISOLATION LEVEL { READ COMMITTED | SERIALIZABLE } ] [ READ WRITE | READ O ...
- \ HTML5开发项目实战:照片墙
html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <ti ...
- MongoDB服务的安装与删除
服务的安装: 在MongoDB的目录下创建两个文件夹 data和logs, 在通过cmd进入bin目录下,执行命令: mongod --dbpath "C:\Program Files\Mo ...
- python 循环中使用多个subplot画子图像(python matplotlib use more than one subplot in loop)
在循环语句中画出多个subplot图像代码如下 http://jonathansoma.com/lede/data-studio/classes/small-multiples/long-explan ...
- 怀旧浪潮来袭,小霸王游戏、windows95......曾经的经典哪些能戳中你的心怀?
随着前两天上架的 Rewound 在 iPhone 上复刻了 iPod Classic为大家掀起一场怀旧浪潮,那么除了 Rewound还有什么经典?今天我们就来怀旧一下那些曾经的经典.80经典小霸王游 ...
- 【leetcode】701. Insert into a Binary Search Tree
题目如下: Given the root node of a binary search tree (BST) and a value to be inserted into the tree, in ...
- Android Studio 打包生成正式apk(XXX-release.apk)的两种方式
{ 方式一:使用Android Studio生成1.点击Build->Generate Signed apk,首次点击可能会提示输入操作系统密码2.弹出如下对话框,还没有生成过keystore ...
- 【Vue】vue的双向绑定原理及实现
vue数据双向绑定是通过数据劫持结合发布者-订阅者模式的方式来实现的,那么vue是如果进行数据劫持的,我们可以先来看一下通过控制台输出一个定义在vue初始化数据上的对象是个什么东西. 代码: var ...
- 如何更改PHPCMS网站后台标题(title)
打开PHPCMS安装目录,选择phpcms 然后选择Languages目录,打开. 打开目录后,选择zh-cn目录,选择admin.lang.php用editPlus打开,将第九行后面的引号中的内容换 ...