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 ...
随机推荐
- 58.Partition Equal Subset Sum(判断一个数组是否可以分成和相等的两个数组)
Level: Medium 题目描述: Given a non-empty array containing only positive integers, find if the array c ...
- ArcGIS 面要素缝隙孔洞检查代码 C# GP
public class PolygonGapChecker : CheckProgressMessageSender, IChecker, ICheckProgressChangeEvent { p ...
- C#编程—第四天
五一放假三天 ······续写第三天的if else-if.if语句的嵌套.很多例题(还有很多没有整理好的) 5.4下午初步学习循环语句for 老师布置了几个小练习题 循环语句 循环:可以反复执行某段 ...
- python mqtt 客户端实现
安装paho-mqtt pip install paho-mqtt -i http://pypi.douban.com/simple --trusted-host pypi.douban.com py ...
- WebAPI 生成验证码
private HttpResponseMessage CreateCheckCodeImage(string checkCode) { HttpResponseMessage result = ne ...
- 关于Django路由层简单笔记
Django—路由层 URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表:你就是以这种方式告诉Django,对于客户端发来的某个U ...
- 使用Makefile编译Erlang
#配置选项,可以是DEBUG和RELEASE CONFIG ?= RELEASE #语言配置,可以是chs(简体中文).cht(繁体中文)等等 Region ?= chs #源文件目录 SOURCE_ ...
- eclipse 附加进程调试java程序(转)
转自:http://blog.csdn.net/zhoushenghuang/article/details/54485645 第一步,启动Java程序时需要设置JVM参数 右击java项目-> ...
- JavaSE---Runtime类
1.概述 1.1 Runtime类 代表 java程序运行时环境: 1.2 Runtime类 提供的类方法: getRuntime():获取Runtime实例: gc():通知垃圾回收器回收资源: ...
- 推荐五个java基础学习网站,小白必备
不知道去哪找java基础资料?推荐几个学习网站,小白必备 Java经过20多年的发展,仍然是世界上最受欢迎的编程语言之一,有无限多种方法使用Java.拥有庞大的客户群.并且java应用范围很广,基本只 ...