【Pascal's Triangle II 】cpp
题目:
Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return [1,3,3,1].
Note:
Could you optimize your algorithm to use only O(k) extra space?
代码:
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> ret(rowIndex+,);
for ( int i=; i<=rowIndex; ++i )
{
for (int j=i-; j>; --j)
{
ret[j] = ret[j] + ret[j-];
}
}
return ret;
}
};
tips:
采用滚动数组技巧,可以缩减空间复杂度。
==========================================
第二次过这道题,题意一开始没有看清,改了一次AC了。
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> ret(rowIndex<?:rowIndex+,);
ret[] = ;
for ( int i=; i<=rowIndex; ++i )
{
for ( int j=i; j>; --j )
{
ret[j] = ret[j-] + ret[j];
}
}
return ret;
}
};
【Pascal's Triangle II 】cpp的更多相关文章
- leetcode 【 Pascal's Triangle II 】python 实现
题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [ ...
- 【Linked List Cycle II】cpp
题目: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. ...
- 【Reverse Linked List II】cpp
题目: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1- ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- 【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】34、119题,Pascal's Triangle II
package y2019.Algorithm.array; import java.util.ArrayList; import java.util.List; /** * @ProjectName ...
- 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, ...
- 28. Triangle && Pascal's Triangle && Pascal's Triangle II
Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...
- Pascal's Triangle,Pascal's Triangle II
一.Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, giv ...
随机推荐
- 小程序wx.request的POST方法的参数传输服务器接收不到
这是API里面的例子: 而实际这样,服务端拿到的是空值. 将header更改一下,application/x-www-form-urlencoded,则可以让服务器收到数据
- [论文理解]关于ResNet的进一步理解
[论文理解]关于ResNet的理解 这两天回忆起resnet,感觉残差结构还是不怎么理解(可能当时理解了,时间长了忘了吧),重新梳理一下两点,关于resnet结构的思考. 要解决什么问题 论文的一大贡 ...
- 1993: C语言实验——最值
1993: C语言实验——最值 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 1541 Solved: 727[Submit][Status][Web ...
- Python -函数的参数定义
一.函数的参数有四种,位置参数.默认参数.可变参数和关键字参数 def func(x, y=0, *arg, **args): '''x为位置参数 y有默认值 *arg为可变参数 **args为关键字 ...
- ztree3.5中checkbox无法取消选中的问题解决
使用jquery的tree插件ztree,用到复选框的功能 var setting = { check: { enable: true }, data: { simpleData: { enable: ...
- ASP.NET中刷新分页
1,第一次全部把数据加载到内存中,然后再做分页,性能差,不推荐. 2,GridView自带分页 3,AspNetPager分页控件 这个是第三分控件需要下载,很好用 4,自己写分页 前三种就不介绍如 ...
- C#中 property 与 attribute的区别?
C#中 property 与 attribute的区别?答:attribute:自定义属性的基类;property :类中的属性
- caffe怎么把全连接层转成convolutional层
caffe中有把fc层转化为conv层的,其实怎么看参数都是不变的,对alex模型来说,第一个fc层的参数是4096X9216,而conv的维度是4096x256x6x6,因此参数个数是不变的,只是需 ...
- git和svn的混用
服务器上的项目是使用svn进行管理的. 本来本地的项目也是通过svn进行管理的,但是后来使用svn的分支功能进行项目的测试/新功能等等时,总是会出现各种各样的问题,遂转投git. 因为git的分支机制 ...
- mysql 自增主键为什么不是连续的?
由于自增主键可以让主键索引尽量地保持递增顺序插入,避免了页分裂,因此索引更紧凑 MyISAM 引擎的自增值保存在数据文件中 nnoDB 引擎的自增值,其实是保存在了内存里,并且到了 MySQL 8.0 ...