描述

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. 【scratch3.0教程】1.2 下载安装scratch

    第2课  下载安装Scratch 1 . 什么是Scratch?                                Scratch将程序语言设计成一块块积木,你只要用拖拉的方式,将程序积木 ...

  2. PB 报表数值列加%

  3. VS使用日常

    一.快捷键 1.Ctrl R+E    选中变量快捷自动生成属性

  4. 你有自信写while(true)吗?

    每次写while(true)的时候会不会心虚? 特别逻辑稍微复杂一点

  5. native function 'Window_sendPlatformMessage' (4 arguments) cannot be found

    https://github.com/pauldemarco/flutter_blue/issues/140 https://github.com/flutter/flutter/issues/168 ...

  6. vue中如何判断checkbox是否选中

    console.log(event.target.checked)     例:  

  7. Android著名开源库

    UI方面 1.绘制图表MPAndroidChart.hellocharts: https://github.com/PhilJay/MPAndroidChart https://github.com/ ...

  8. 只要200行JavaScript代码,就能把特斯拉汽车带到您身边

    Jerry的前一篇文章 如何使用JavaScript开发AR(增强现实)移动应用 (一) 介绍了用React-Native + ViroReact开发增强现实应用的一些预备知识. 本文咱们开始进入增强 ...

  9. 学习python的日常7

    ---恢复内容开始--- 正则表达式: 在正则表达式中,用\d可以匹配一个数字,\w可以匹配一个字母或数字,'.'可以匹配任意字符,用*表示任意个字符,用+表示至少一个字符,用?表示0个货一个字符,用 ...

  10. c# FileStream 类构造函数