[leetcode.com]算法题目 - Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
Return
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
class Solution {
public:
vector<vector<int> > generate(int numRows) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if( == numRows){
vector<vector<int> > result;
return result;
}
vector<vector<int> > result(numRows);
vector<int> first(,);
result[] = first;
for(int i=;i<numRows;i++){
result[i] = nextTriangle(result[i-]);
}
return result;
}
vector<int> nextTriangle(vector<int> a){
int k = a.size();
vector<int> next(k+);
for(int i=;i<k+;i++){
if(==i){
next[i] = a[i];
continue;
}
if(k==i){
next[i] = a[k-];
continue;
}
next[i] = a[i-]+a[i];
}
return next;
}
};
我的答案
思路:创建一个函数,使用上一行的vector去计算下一行的vector,然后反复调用即可。需要尤其注意输入的值为0时候的情况。
[leetcode.com]算法题目 - Pascal's Triangle的更多相关文章
- 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(118) Pascal's Triangle
题目 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, R ...
- LeetCode Array Easy 119. Pascal's Triangle II
Description Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's tria ...
- LeetCode Array Easy 118. Pascal's Triangle
Description Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. I ...
- 【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&Python] Problem 118. Pascal's Triangle
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's t ...
- LeetCode算法题-Pascal's Triangle II(Java实现)
这是悦乐书的第171次更新,第173篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第30题(顺位题号是119).给定非负索引k,其中k≤33,返回Pascal三角形的第k ...
- LeetCode算法题-Pascal's Triangle(Java实现)
这是悦乐书的第170次更新,第172篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第29题(顺位题号是118).给定非负整数numRows,生成Pascal三角形的第一个 ...
- [leetcode.com]算法题目 - Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
随机推荐
- Python中的实例方法、类方法、静态方法和普通方法
为了辨析它们的差别,写了如下代码: class C: def self_method(self, a): return a @classmethod def class_method(cls, a): ...
- 783. Minimum Distance Between BST Node
方法一,非递归方法,中序遍历 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *l ...
- 2019.01.24 NOIP训练 旅行(轮廓线dp)
传送门 题意简述: 给一个n∗mn*mn∗m的有障碍的网格图,问你从左上角走到左下角并覆盖所有可行格子的路径条数. 思路: 路径不是很好算. 将图改造一下,在最前面添两列,第一列全部能通过,第二列只有 ...
- hashable/iterable与orderable
################ # hashable协议 # ################ # 一个对象能被称为hashable,它必须实现__hash__与_eq__方法: >>& ...
- Educational Codeforces Round 62 E 局部dp + 定义状态取消后效性
https://codeforces.com/contest/1140/problem/E 局部dp + 定义状态取消后效性 题意 给你一个某些位置可以改变的字符串,假如字符串存在回文子串,那么这个字 ...
- orika实现对象复制
1.新建maven工程orika-demo,引入orika依赖,其中pom.xml如下 <?xml version="1.0" encoding="UTF-8&qu ...
- C# 中的委托(Delegate)
委托(Delegate) 是存有对某个方法的引用的一种引用类型变量.引用可在运行时被改变. 委托(Delegate)特别用于实现事件和回调方法.所有的委托(Delegate)都派生自 System.D ...
- IntelliJ IDEA 2017版 使用笔记(十一) Debug操作:IDEA 快捷键
调试功能; 缩短项目时间,调高阅读源码的能力. 一.添加断点,选中一行代码,双击即可生成断点(快捷键:ctrl+F8) 二.单步运行,快捷键:s ...
- vue中的路由嵌套
1定义组件 const Index = { template:` <div>首页</div> ` } const Order = { template:` <div> ...
- 第19章:MongoDB-聚合操作--聚合管道--$limit+$skip
①$limit “$limit”:负责数据的取出个数: “$skip”:数据的跨过个数. ②范例:使用“$limit”设置取出的个数