Given an index k, return the kth row of the Pascal's triangle.

For example, given k = 3,
Return [1,3,3,1].

Note:
Could you optimize your algorithm to use only O(k) extra space?

vector<int> getRow(int rowIndex) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<int> matrix(rowIndex+1);
if(rowIndex < 0) return matrix;
for(int i = 0; i <= rowIndex; ++i)
{
if( i == 0) {matrix[0] = 1;continue;}
int mid = i/2;
int tmpnum = matrix[0];
for(int j = 1; j <= mid; ++j)
{
int tmp = tmpnum + matrix[j];
tmpnum = matrix[j];
matrix[j] = tmp;
}
++mid;
while(mid <= i){
matrix[mid] = matrix[i-mid];
++mid;
}
}
return matrix;
}

leetcode_question_119 Pascal's Triangle II的更多相关文章

  1. 28. Triangle && Pascal's Triangle && Pascal's Triangle II

    Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...

  2. 【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, ...

  3. Pascal's Triangle,Pascal's Triangle II

    一.Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, giv ...

  4. 杨辉三角形II(Pascal's Triangle II)

    杨辉三角形II(Pascal's Triangle II) 问题 给出一个索引k,返回杨辉三角形的第k行. 例如,给出k = 3,返回[1, 3, 3, 1] 注意: 你可以优化你的算法使之只使用O( ...

  5. LeetCode OJ 119. Pascal's Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  6. 118/119. Pascal's Triangle/II

    原文题目: 118. Pascal's Triangle 119. Pascal's Triangle II 读题: 杨辉三角问题 '''118''' class Solution(object): ...

  7. LeetCode: Pascal's Triangle II 解题报告

    Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question Solution ...

  8. 学会从后往前遍历,例 [LeetCode] Pascal's Triangle II,剑指Offer 题4

    当我们需要改变数组的值时,如果从前往后遍历,有时会带来很多麻烦,比如需要插入值,导致数组平移,或者新的值覆盖了旧有的值,但旧有的值依然需要被使用.这种情况下,有时仅仅改变一下数组的遍历方向,就会避免这 ...

  9. LeetCode Pascal's Triangle && Pascal's Triangle II Python

    Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given ...

随机推荐

  1. Linux安装mysql-python库时报错解决办法

    用pip安装mysql-python库的时候遇到如下报错 root@LoidAir:~# pip install mysql-python Collecting mysql-python Using ...

  2. POJ burnside&&polya整理练习

    POJ 2409 Let it Bead 这题就是polya公式的直接套用,唯一麻烦的是置换群的种类数,由于可以翻转,所以除了要加上pow(c,gcd(s,i))这些平面旋转的置换群,还要加上翻转的. ...

  3. BZOJ 3110 ZJOI 2013 K大数查询 树套树(权值线段树套区间线段树)

    题目大意:有一些位置.这些位置上能够放若干个数字. 如今有两种操作. 1.在区间l到r上加入一个数字x 2.求出l到r上的第k大的数字是什么 思路:这样的题一看就是树套树,关键是怎么套,怎么写.(话说 ...

  4. 常用语言api语法Cheat Sheet

    http://overapi.com/jquery/ OverAPI.com Python jQuery NodeJS PHP Java Ruby Javascript ActionScript CS ...

  5. MVC三和,你能辨别它?

    上次我们聊的时间MVC,而之前我们学习过三层.那么我们不禁就要问,他们说的是一回事吗.他们有什么联系吗? 三层架构(3-tier application)通常意义上的三层架构就是将整个业务应用划分为: ...

  6. Java基础知识强化54:经典排序之插入排序(InsertSort)

    1. 插入排序原理图: 算法步骤: 1)将第一待排序序列第一个元素看做一个有序序列,把第二个元素到最后一个元素当成是未排序序列. 2)从头到尾依次扫描未排序序列,将扫描到的每个元素插入有序序列的适当位 ...

  7. Window 10通过网线和Wifi连接树莓派

    几个月前买了个树莓派,扔在一边没有捣鼓,今天搞定了笔记本通过家里的wifi登录树莓派,下面列出设置过程. 实验环境: 网络:只有wifi 材料:笔记本一台(Win10),树莓派一台,EDUP USB无 ...

  8. Windows 右键快速运行命令行

    原文见:右键命令行 - yacper - 博客园 方法一:配置文件夹选项 1 打开人任意文件夹,[工具] --> [文件夹选项] --> [文件类型] --> [(无)资料夹] -- ...

  9. IPointCollection,ISegmentCollection和IGeometryCollection

    Engine 提供了三个主要的几何图形集合接口用于对几何对象的操作,分别是 IPointCollection,ISegmentCollection 和 IGeometryCollection,这些接口 ...

  10. Segment,Path,Ring和Polyline的区别

    这四者当中Segment是最小的单位,具体的构成路线可以分为两个条:Segment-Path-Ring(封闭的Path)Segment-Path-Polyline Segment 和 Path 可以说 ...