leetcode解题报告(23):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]
]
分析
其实就是杨辉三角,以前用队列写过。
为了和numRows相匹配,以变量i代表当前的行数,那么i-1才是当前行的下标。以j代表当前行的第i个元素(这个j是从下标1开始的)。
代码如下:
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<int>ele; //store elements of current line
vector<vector<int>>ret; //store all the lines and as a return variable
if(numRows == 0)return ret; //return an empty vector if numRows == 0
int j = 0; //initialize j
for(int i = 1; i <= numRows; ++i){ //note: i begins from 1,means the first line
ele.push_back(1); //push 1 before do any operators in a line
if(i >= 2){
j = 2;
while(j < i){
//because i starts with 1,so i - 1 is current line,while i - 2 is the line before current
//so as to j
ele.push_back(ret[i - 2][j - 2] + ret[i - 2][j - 1]);
++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;
}
};
leetcode解题报告(23):Pascal's Triangle的更多相关文章
- 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解题报告(24):Pascal's TriangleII
描述 Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [ ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- 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解题报告汇总! All in One!
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...
- 【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.com]算法题目 - Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
随机推荐
- PB Event ID 含义 内容浅析
Event ID 含义 内容浅析 event可以用pb自带的id,自动触发事件,而function就需要你去调用了,返回值多种多样 单选或多选按钮消息(前缀:pbm_bm) pbm_bmgetchec ...
- Spring Cloud Alibaba学习笔记(9) - RocketMQ安装与RocketMQ控制台
搭建RocketMQ 系统环境准备 64位操作系统,推荐使用Linux.Unix.MacOS 64位 JDK1.8+ Maven 3.2.x 适用于Broker服务器的4g +可用磁盘 下载与搭建 下 ...
- Thread.interrupt()源码跟踪
1 JDK源码跟踪 // java.lang.Thread public void interrupt() { if (this != Thread.currentThread()) checkAcc ...
- 2.MVC基础-Model概述(思维导图)
已思维导图形式,便于记忆和补充
- JTAG各类接口针脚定义、含义以及SWD接线方式
JTAG有10pin的.14pin的和20pin的,尽管引脚数和引脚的排列顺序不同,但是其中有一些引脚是一样的,各个引脚的定义如下. 一.引脚定义 Test Clock Input (TCK) --- ...
- weblogic jdbc 相关概念介绍
weblogic jdbc 是什么? 如何配置? 常见问题? 如何监控?
- RocketMQ——角色与术语详解
原文地址:http://jaskey.github.io/blog/2016/12/15/rocketmq-concept/ RocketMQ——角色与术语详解 2016-12-15 THU 15:4 ...
- hashmap,hashtable,concurrenthashmap多线程下的比较(持续更新)
1.hashMap 多线程下put会造成死循环,主要是扩容时transfer方法会造成死循环. http://blog.csdn.net/zhuqiuhui/article/details/51849 ...
- Linux命令——column
参考:Viewing Linux output in columns 功能 column命令把他的输入格式化多列显示.输入可以是文件,也可以是标准输入. 列优先,从左到右 显示的时候首先填满最左列,然 ...
- 25道Shell面试题
1. 用sed修改test.txt的23行test为tset: sed –i ‘23s/test/tset/g’ test.txt 2. 查看/web.log第25行第三列的内容. sed –n ‘2 ...