【一天一道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- ...
随机推荐
- Machine Learning From Scratch-从头开始机器学习
Python implementations of some of the fundamental Machine Learning models and algorithms from scratc ...
- 详解linux进程间通信-管道 popen函数 dup2函数
前言:进程之间交换信息的唯一方法是经由f o r k或e x e c传送打开文件,或通过文件系统.本章将说明进程之间相互通信的其他技术-I P C(InterProcess Communication ...
- ubuntu远程桌面连接命令rdesktop连接windows远程桌面详解
sudo apt-get install rdesktoprdesktop 124.42.120.174:1433 呵呵,连接成功了. -f 全屏-a 16位色默认端口是3389(linux 22 s ...
- Ubuntu16.04下安装jdk1.8过程
笔者环境:腾讯云服务器 Ubuntu16.04 x64 一 . 去oracle官网下载对应的jdk 下载地址:http://www.oracle.com/technetwork/java/javase ...
- python:浅析python 中__name__ = '__main__' 的作用(转载)
每次看文章的源码时,Python的主程序中都会看到开头有这个,查了一下作用:https://www.cnblogs.com/alan-babyblog/p/5147770.html. 讲的很详细,就直 ...
- ACM Wooden Stricks
有一堆n根木棍.每根棍子的长度和重量是预先知道的. 这些棒子将被木工机器逐一加工..它需要一些时间,称为安装时间,用于机器准备加工棒.设置时间与机器中的清洁操作和更换工具和形状相关联.木工机械的安装时 ...
- MySQL系列教程(五)
MyCAT MyCat是基于阿里开源的Cobar产品而研发,Cobar的稳定性.可靠性.优秀的架构和性能以及众多成熟的使用案例使得MYCAT一开始就拥有一个很好的起点,站在巨人的肩膀上,我们能看到更远 ...
- Bash shell中的位置参数$#,$*,$@,$0,$1,$2...及特殊参数$?,$-等的含义
http://hi.baidu.com/lolorosa/blog/item/5775a608bd670d33b0351da7.html $# 是传给脚本的参数个数 $@ 是传给脚本的所有参数的列表 ...
- OpenCV 2.x/3.x 随机初始化矩阵
简介 在测试算法的时候,或者某些算法需要使用随机数,本文介绍如何使用OpenCV的随机数相关功能. 主要内容: 1. cv::RNG类 -- random number generator 2. cv ...
- cassandra 3.x官方文档(4)---分区器
写在前面 cassandra3.x官方文档的非官方翻译.翻译内容水平全依赖本人英文水平和对cassandra的理解.所以强烈建议阅读英文版cassandra 3.x 官方文档.此文档一半是翻译,一半是 ...