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 that the row index starts from 0.
Example:
Input: 3
Output: [1,3,3,1]
Follow up:
Could you optimize your algorithm to use only O(k) extra space?
要完成的函数:
vector<int> getRow(int rowIndex)
说明:
1、这道题给定一个行数rowIndex,要求返回给定行数那一行的帕斯卡三角形,结果存储在vector中。要求空间复杂度不超过O(rowIndex)。
2、做了前面的帕斯卡三角形的生成那道题目,实现这道题目也就易如反掌了。
同样的方法,我们逐行生成每一行的帕斯卡三角形,到达指定行数时,停下来,返回vector就可以了。
代码如下:
vector<int> getRow(int rowIndex)
{
vector<int>res={1};
int i;
while(rowIndex--)//如果rowIndex==0,那么不会进入循环,如果rowIndex==1,进入循环一次。
{//while(rowIndex--)这种写法,是先判断rowIndex是不是0,如果不是那么进入循环,最后再减一;如果是,那么不进入循环。
i=res.size()-1;
while(i>0)
{
res[i]=res[i]+res[i-1];
i--;
}
res.push_back(1);
}
return res;
}
上述代码实测3ms,beats 80.79% of cpp submissions。
leetcode-119-Pascal's Triangle II(生成某一行的帕斯卡三角形)的更多相关文章
- [LeetCode] 119. Pascal's Triangle II 杨辉三角 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 ...
- 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, ...
- C#解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 ...
- LeetCode 119 Pascal's Triangle II
Problem: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Ret ...
- leetcode 119 Pascal's Triangle II ----- java
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
- Java [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 ...
- Java for 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 数论递推
杨辉三角,这次要输出第rowIndex行 用滚动数组t进行递推 t[(i+1)%2][j] = t[i%2][j] + t[i%2][j - 1]; class Solution { public: ...
- 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- ...
随机推荐
- Opencv 亚像素级别角点检测
Size winSize = Size(5,5); Size zerozone = Size(-1,-1); TermCriteria tc = TermCriteria(TermCriteria:: ...
- 274. H-Index论文引用量
[抄题]: Given an array of citations (each citation is a non-negative integer) of a researcher, write a ...
- php实现二分查找法
二分查找法需要数组是一个有序的数组 假设我们的数组是一个递增的数组,首先我们需要找到数组的中间位置. 一.要知道中间位置就需要知道起始位置和结束位置,然后取出中间位置的值来和我们的值做对比. 二.如果 ...
- spring4-2-bean配置-7-Spring表达式语言SpEL
- Windows服务器常用的性能计数器
Windows常用性能计数器总结 基础监控: 1.SQL Server Buffer: Buffer Cache Hit Ratio 这是一个很重要查看内存是否不足的参数.SQL Server Buf ...
- C程序之包含头文件
在C程序中包含文件有以下两种方法: 方法一:#include<XXX.h> 这里的XXX一般是改动较小的标准库,用符号"<"和">"将要 ...
- 洛谷 P3659 [USACO17FEB]Why Did the Cow Cross the Road I G
//神题目(题目一开始就理解错了)... 题目描述 Why did the cow cross the road? Well, one reason is that Farmer John's far ...
- transition与animation的区别
transition需要事件触发,animation可以直接自动触发,而且功能上更为强大,包括可以设置不同时间段的动画规则,还有状态的控制,事件等等.
- shell 编程 变量
转自:http://blog.csdn.net/qq504196282/article/details/52994249 shell之变量和引用 分类:SHELL编程基础 (470) (0) 举报 ...
- 简述各大 Linux 发行版,有主观,不完全,望见谅
只罗列当前热门的linux发行版 更多关于 Linux 以及 Linux 衍生版的内容可以参阅 中文wiki Debian 系 Debian:开源社区的代表性 linux 系统,每2年一次更新,现在的 ...