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 ...
随机推荐
- Bean管理学习笔记
1.BeanFactory 介绍 1.1 首先什么是Bean? 1.Bean在Spring技术中是基于组件 2.他是Spring容器管理的最基本最常见的单元.在spring的应用场合中,bean可以是 ...
- 2.9_Database Interface ADO结构组成及连接方式实例
说通俗点OLE DB和ODBC都是最底层的东西,而ADO对象给我们提供了一个“可视化”和应用层直接交互的组件,ADO对象T通过OLE DB间接取得数据库中的数据,如下图: 从上面看出,可以说ADO是应 ...
- 【洛谷 P2597】 [ZJOI2012]灾难(LCA)
题目链接 考虑建一棵树,使一个生物灭绝时他的子树都会灭绝,显然这样答案就是以每个点为根的子树大小-1. 为什么原图不是一棵树,因为一个生物可能会以多个生物为食,所以按拓扑序来建树,把每个遍历到的点的父 ...
- webRTC client 源码环境工具配置
以下操作真实实验过,安卓,苹果均可成功 环境 Mac 10.13.6 Xcode 11 翻墙代理:Lantern 专业版 Python 2.7.10 设置 git 代理 #设置git代理 $ git ...
- iOS - 静态库的导入和头文件路径的设置
开发过程中,免不了要用到一些第三方或者是别人写好的一些Demo,这时就经常会用到别人写的一些静态库(.a文件)和n多个头文件(.h文件),这些文件不是简单地把它们拉到自己的工程里面就行的,还要设置一些 ...
- PHP 的一款http请求封装类
<?php namespace hisi; class Http { protected static $userAgent = [ 'Mozilla/5.0 (Windows NT 6.1; ...
- 分享个免费的svn平台
平时在工作中难免会用到svn,但是要自己搭建一个,未免成本太高,近来,本人接触到一个免费的svn平台(可能大神们早就发现了),个人使用还是足够了. 地址:https://svnbucket.com 相 ...
- vue 解决ios编辑器输入框不能拉起
一.问题描述:Android .pc.下可以正常使用,在ios下可以拉起输入框但是无法输入 <div contenteditable="true" ></div& ...
- Tomcat启动时一闪而过,看不多错误信息解决方案
转自:https://wangxh89.iteye.com/blog/1824806 有时Tomcat的启动窗口一闪而过,根本就看不出启动过程中发生了什么错误.这中间的原因有好多种,最常见的解决办法就 ...
- II、Vue的项目目录结构 一些语法
Vue目录结构 这是某闭源项目的web端目录结构: 目录解析: -目录/文件 - build 项目构建(webpack)相关代码 config 配置目录.端口号:也有默认的 node_modules ...