描述

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的更多相关文章

  1. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  2. leetcode解题报告(2):Remove Duplicates from Sorted ArrayII

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  3. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  4. leetcode解题报告(23):Pascal's Triangle

    描述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, R ...

  5. leetCode解题报告5道题(六)

    题目一: Longest Substring Without Repeating Characters Given a string, find the length of the longest s ...

  6. LeetCode解题报告—— Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. logback 日志相关

    日志相关: logback:https://www.cnblogs.com/gavincoder/p/10091757.html https://www.cnblogs.com/hhhshct/p/9 ...

  2. 14-4 ADO.NET简介

    微软数据访问方式历史阶段 ①ODBC(Open Database Connectivity)是第一个使用SQL访问不同关系数据库的访问技术.使用ODBC应用程序能够通过单一的命令操作不同的数据库,而开 ...

  3. C# Java的加密的各种折腾

    24位加密 Java public class DESUtil { private static final String KEY_ALGORITHM = "DESede"; pr ...

  4. 【洛谷 P4052】 [JSOI2007]文本生成器(AC自动机,DP)

    题目链接 AC自动机上dp第一题嗷. 如果直接求可读文本的数量,显然要容斥,不好搞. 于是考虑求不可读文本的数量,再用\(26^m\)减去其即可. 建出AC自动机,如果一个节点为单词结尾或其fail链 ...

  5. window 命令行强制删除文件、文件夹

    1. 强制删除文件文件夹和文件夹内所有文件 rd/s/q D:\app 2. 强制删除文件,文件名必须加文件后缀名 del/f/s/q D:\app.txt

  6. SSM框架之MyBatis入门介绍

    一.什么是MyBatis? MyBatis源自Apache的iBatis开源项目, 从iBatis3.x开始正式更名为MyBatis.它是一个优秀的持久层框架. 二.为什么使用MyBatis? 为了和 ...

  7. 笔谈OpenGL ES(一)

    现在图形类.视频类app越来越多,学习OpenGL ES是很有必要的,作为程序员是有必要做技术积累的.现在做播放器开发的工作,正好也涉及这块,那就好好学一学. CSDN上有套教程不错,OpenGL E ...

  8. 阿里P7整理20个非常有用的Java程序片段,你知道吗?

    1.字符串有整型的相互转换 String a = String.valueOf(2);  //integer to numeric string   int i = Integer.parseInt( ...

  9. python(time/random模块)

    一.Time模块 1.时间戳 时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数 最早出现的UNIX操作系统考虑到计算机 ...

  10. ansible之基础篇(二)

    软件相关模块 yum rpm和yum的区别 rpm:redhat package manager yum 可以解决依赖关系 yum 源配置 使用yum下载时需要先下载epel [epel] name= ...