描述

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. MediaPlayer的prepareAsync不回调onPrepared问题

    一.问题排查 debug调试的framework日志信息: 根据日志,进入MediaPlayer的framework层看源码 STEP 1 先从 V/MediaPlayerNative: constr ...

  2. Java的基础类型笔记

    数据类型       大小 byte(字节)    1(8位) shot(短整型)       2(16位) int(整型)       4(32位) long(长整型)       8(32位) f ...

  3. Net core 2.x 升级 3.0 使用自带 System.Text.Json 时区 踩坑经历

    .Net Core 3.0 更新的东西很多,这里就不多做解释了,官方和博园大佬写得很详细 关于 Net Core 时区问题,在 2.1 版本的时候,因为用的是 Newtonsoft.Json,配置比较 ...

  4. ADO.NET 七(一个例子)

    通过一个完整的实例实现课程信息管理功能的操作,包括查询.修改.删除课程信息等操作. 1) 创建课程信息表 create table StuCourse ( id int primary key ide ...

  5. OO第三单元(地铁,JML)单元总结

    OO第三单元(地铁,JML)单元总结 这是我们OO课程的第二个单元,这个单元的主要目的是让我们熟悉并了解JML来是我们具有规格化编程架构的思想.这个单元的主题一开始并不明了,从第一次作业的路径到第二次 ...

  6. Mock测试框架(Mockito为例)

    在做单元测试的时候,有的时候用到的一些类,我们构造起来不是那么容易,比如HttpRequest,或者说某个Service依赖到了某个Dao,想构造service还得先构造dao,这些外部对象构造起来比 ...

  7. Linux命令——du

    参考:10 Useful du (Disk Usage) Commands to Find Disk Usage of Files and Directories 前言 du(Disk Usage), ...

  8. netty实现websocket发送文本和二进制数据

    原文:https://huan1993.iteye.com/blog/2433552 最近在学习netty相关的知识,看到netty可以实现 websoket,因此记录一下在netty中实现webso ...

  9. 用java刷剑指offer(数字在排序数组中出现的次数)

    题目描述 统计一个数字在排序数组中出现的次数. 牛客网链接 java代码 //看见有序就用二分法 public class Solution { public int GetNumberOfK(int ...

  10. 用js刷剑指offer(第一个只出现一次的字符)

    题目描述 在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写). 牛客网链接 js代码 fu ...