LeetCode 119. Pascal's Triangle II
来源:互联网 发布:v2ex 源码下载 编辑:程序博客网 时间:2024/06/05 15:17
119. Pascal’s Triangle II
一、问题描述
Given an index k, return the *k*th row of the Pascal’s triangle.
Could you optimize your algorithm to use only O(k) extra space?
二、输入输出
For example, given k = 3,
Return [1,3,3,1]
.
三、解题思路
递归解法
- 要求空间复杂度为o(k) 是本题的难点,想要从头一点点遍历来产生怕是不行了
- 像这种按照一定的规律来产生数组的,可以考虑使用递归
- 递归调用自己产生
第k-1行
的结果,根据这个结果可以产生第k行
的结果 - 退出条件设置为rowIndex为0或者1
class Solution {public: vector<int> getRow(int rowIndex) { if(rowIndex <= 0) return vector<int>(1,1); if(rowIndex == 1) return vector<int>(2, 1); vector<int> lastLine = getRow(rowIndex - 1); vector<int> ret; ret.push_back(1); for (int i = 1; i <= (rowIndex-1); ++i) { ret.push_back(lastLine[i - 1] + lastLine[i]); } ret.push_back(1); return ret; }};
阅读全文
0 0
- [LeetCode]118. Pascal's Triangle&119. Pascal's Triangle II
- LeetCode 119. Pascal's Triangle II
- 119. Pascal's triangle II Leetcode Python
- [LeetCode]119.Pascal's Triangle II
- [Leetcode] 119. Pascal's Triangle II
- [leetcode] 119.Pascal's Triangle II
- 【leetcode】119. Pascal's Triangle II
- 【Leetcode】119. Pascal's Triangle II
- 119. Pascal's Triangle II LeetCode
- [LeetCode]119. Pascal's Triangle II
- Leetcode 119. Pascal's Triangle II
- LeetCode *** 119. Pascal's Triangle II
- 【leetcode】119. Pascal's Triangle II
- LeetCode 119. Pascal's Triangle II
- leetcode-119. Pascal's Triangle II
- LeetCode 119. Pascal's Triangle II
- 【leetcode】119. Pascal's Triangle II
- LeetCode - 119. Pascal's Triangle II
- 用R语言igraph库实现表自动关联
- 新闻客户端
- UICollectionView reloadData不走cellForRowAtIndexPath的问题
- URL标准概念
- 3224: Tyvj 1728 普通平衡树 P3369 【模板】普通平衡树(Treap/SBT)Treap
- LeetCode 119. Pascal's Triangle II
- bzoj 3771 Triple
- MDK/keil自动补齐代码的设置方法
- 简述如何获取请求方的IP地址,及php相关获取函数解析
- JS 二进制计算,如何得到一个负数的二进制表示
- 设置QTableWidgetItem不可编辑和可编辑状态
- 编程第二十七天
- IntelliJIDEA软件注册破解工具,Jetbrains旗下IntelliJIDEA、WebStorm、PhpStorm均可激活
- hdu1595—find the longest of the shortest(dijkstra+枚举)