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. Storm概念介绍

    Storm核心概念如下:  1.Tuple:元组                Tuple即元组,是一个拓扑Topology中的Spout和Bolt组件之间数据传递的基本单元.元组中的字段可以是任何类 ...

  2. phpstorm + xdebug 配置

    PHPSTORM版本 : 8.0.1 PHP版本 : 5.6.2 把php-xdebug.dll复制到xamapp/php/ext目录下,打开php.ini配置如下参数 [xdebug] zend_e ...

  3. Android设备中实现Orientation Sensor(图)兼谈陀螺仪

    设备中的三自由度Orientation Sensor就是一个可以识别设备相对于地面,绕x.y.z轴转动角度的感应器(自己的理解,不够严谨).智能手机,平板电脑有了它,可以实现很多好玩的应用,比如说指南 ...

  4. i++与++i的区别,使用实例说明

    /** * 类名:TEST.java<br> * <p> * 功能:i++与++i的区别,使用实例说明 * </p> * * @Author:<a href= ...

  5. 升级Android ADT 和SDK

    因为眼下从事android开发工作,所以升级了下Android SDK和eclipse ADT插件 一.更新ADT 1.Eclipse中打开Help->Install New Software. ...

  6. xss漏洞校验

    Xss(跨站脚本攻击)大家应该已经都有所了解,下面讲讲怎样查找xss漏洞吧. 确定xss漏洞的基本方法是使用攻击字符串来验证的,例如”><script>alert(document. ...

  7. Sqoop是一款开源的工具,主要用于在HADOOP(Hive)与传统的数据库(mysql、oracle...)间进行数据的传递

    http://niuzhenxin.iteye.com/blog/1706203   Sqoop是一款开源的工具,主要用于在HADOOP(Hive)与传统的数据库(mysql.postgresql.. ...

  8. 命令行运行android模拟器

    创建模拟器 android create avd --name avd_4.1 --target "android-16" --abi armeabi-v7a Android 4. ...

  9. Javascript和jQuery WordPress 图片轮播插件, 内容滚动插件,前后切换幻灯片形式显示

    用于在有限的网页空间内展示一组产品图片或者照片,同时还有非常吸引人的动画效果.本文向大家推荐12款实用的 jQuery 图片轮播效果插件,帮助你在你的项目中加入一些效果精美的图片轮播效果,希望这些插件 ...

  10. jquery之营销系统(会员促销)

    var appPath = getAppPath(); var cnt = 0; var loadCnt = 0; $(function() { $("#opreateHtml") ...