leetcode解题报告(24):Pascal's TriangleII
描述
Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return [1,3,3,1].
分析
先构造了一个杨辉三角,然后返回这个杨辉三角的最后一组值,没超时就万事大吉了...
感觉可以用递归写,但是嫌麻烦,放弃了。
代码如下:
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int>ele; //store elements of current line
vector<vector<int>>ret; //store all the lines and as a return variable
if(rowIndex < 0)return ele; //return an empty vector if rowIndex == 0
int j = 0; //initialize j
for(int i = 0; i <= rowIndex; ++i){
ele.push_back(1); //push 1 before do any operators in a line
if(i >= 1){
j = 1;
while(j < i){
ele.push_back(ret[i - 1][j - 1] + ret[i - 1][j]);
++j;
}
ele.push_back(1); //the last number in a line is also 1
}
ret.push_back(ele);
ele.clear(); //each time we finish a line,clear this vector
}
return ret[rowIndex];
}
};
leetcode解题报告(24):Pascal's TriangleII的更多相关文章
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- leetcode解题报告(23):Pascal's Triangle
描述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, R ...
- leetCode解题报告5道题(六)
题目一: Longest Substring Without Repeating Characters Given a string, find the length of the longest s ...
- LeetCode解题报告—— Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- LeetCode解题报告—— Search in Rotated Sorted Array & Search for a Range & Valid Sudoku
1. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated(轮流,循环) at so ...
- LeetCode解题报告—— 2 Keys Keyboard & Longest Palindromic Substring & ZigZag Conversion
1. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You ...
- LeetCode解题报告—— 1-bit and 2-bit Characters & 132 Pattern & 3Sum
1. 1-bit and 2-bit Characters We have two special characters. The first character can be represented ...
随机推荐
- jQuery框架"风云榜"案例
<title>电影风云榜</title> <style> /*清空默认样式*/ *{padding:0;margin:0;border:0;list-style:n ...
- python爬虫-喜马拉雅_晚安妈妈睡前故事
这里先说下思路: 1.首先要获取当前书的音频信息 '''获取当前书的音频信息''' all_list = [] for url in self.book_url: r = requests.get(u ...
- Jmeter_数据库
1.准备一个有测试数据表的mysql数据库 2.在测试计划面板点击“浏览..." 按钮,将你的JDBC驱动添加进来. 需要安装插件 mysql-connector-jav ...
- python线程(转)
转自:https://www.cnblogs.com/huxi/archive/2010/06/26/1765808.html
- application.yml报错:a global security auto-configuration is now provided
报错原因: Spring Boot 1.5升级到2.0改动 security开头的配置及management.security均已过期 Actuator 配置属性变化 Endpoint变化 参考来源: ...
- lucene中Field简析
http://blog.csdn.net/zhaoxiao2008/article/details/14180019 先看一段lucene3代码 Document doc = new Document ...
- SpringICO和DI区别
学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...
- Euraka适合初学者的简单小demo
1. 创建父工程:父工程的的打包形式该为pom,删除其余无关的文件 修改父工程的pom文件内容如下: <?xml version="1.0" encoding="U ...
- maccms 山寨站点 V10 后门
经验证:www.maccmsv10应该是个山寨站 -------------------- 前言 苹果CMS是国内优秀的开源PHP建站系统,擅长电影程序影视系统这一块,在主流建站系统中特色鲜明,以灵活 ...
- websocket趣说_转
websocket协议:https://tools.ietf.org/html/rfc6455 作者:Ovear链接:https://www.zhihu.com/question/20215561/a ...