【一天一道LeetCode】#119. Pascal's Triangle II
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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?
(二)解题
题目大意:求杨辉三角的第i行数。
和上题一样:【一天一道LeetCode】#118. Pascal’s Triangle.
只不过这题只需要返回第i行数。这里可以用两个vector,一个记录上一行的数,一个存储本行的数。
代码如下:
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> pre;
vector<int> temp;
int n = 0;
while(n<=rowIndex)
{
temp.clear();
for(int i = 0 ; i < n+1 ; i++)
{
if(i==0||i==n) temp.push_back(1);//首尾为1
else temp.push_back(pre[i-1]+pre[i]);//其他行为上一行第i-1个加上第i个
}
pre = temp;//记录上一行
n++;
}
return temp;
}
};
【一天一道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, ...
- 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 ...
- 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 ...
- 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- ...
随机推荐
- FreeMarker学习教程
copy自http://demojava.iteye.com/blog/800204 以下内容全部是网上收集: FreeMarker的模板文件并不比HTML页面复杂多少,FreeMarker模板文件主 ...
- event工具集
eventTool = { // 页面加载完成后 readyEvent : function(fn) { if (fn==null) { fn=document; } var oldonload = ...
- 转:Kafka 客户端TimeoutException问题之坑
原文出自:http://www.jianshu.com/p/2db7abddb9e6 各种TimeoutException问题 会抛出org.apache.kafka.common.errors.Ti ...
- JS基础(二)
一.JS中的循环结构 循环结构的执行步骤 1.声明循环变量: 2.判断循环条件: 3.执行循环体操作: 4.更新循环变量 然后,循环执行2-4,直到条件不成立时,跳出循环. while循环()中的表达 ...
- 解决问题redis问题:ERR Client sent AUTH, but no password is set
是的,这又是一个坑爹的问题. 明明在redis.conf中设置了密码,而且redis还启动了,为什么说没有密码呢? 大家都知道linux下启动redis有很多种方法, 其中有 ./redis-serv ...
- Python中capitalize()与title()的区别
capitalize()与title()都可以实现字符串首字母大写.主要区别在于:capitalize(): 字符串第一个字母大写title(): 字符串内的所有单词的首字母大写 例如: >&g ...
- ng-book札记——内置指令
Angular提供了一些内置指令(directive),这些添加在HTML元素(element)上的特性(attribute)可以赋予更多的动态行为. NgIf ngIf指令用于在某个条件下显示或者隐 ...
- jQuery 遍历 – 同胞(siblings)
同胞拥有相同的父元素. 通过 jQuery,您能够在 DOM 树中遍历元素的同胞元素. 在 DOM 树中水平遍历 有许多有用的方法让我们在 DOM 树进行水平遍历: siblings() next() ...
- django的流程和命令行工具
django实现流程django #安装: pip3 install django 添加环境变量 #1 创建project django-admin startproject mysite ---my ...
- How To Automate Disconnection of Idle Sessions
***Checked for relevance on 30-Apr-2012*** goal: How to automate disconnection of idle sessions fact ...