Problem:

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?

Summary:

返回杨辉三角(帕斯卡三角)的第k行。

Solution:

1. 若以二维数组的形式表示杨辉三角,则可轻易推算出row[i][j] = row[i - 1][j - 1] + row[i - 1][j], 但在这道题中只要求返回一行,消耗空间是完全没有必要的。

我们可知若已知第i行的所有数值,则第i+1行的数值可以由row[j] = row[j - 1] + row[j]计算出,但若正向计算,则计算row[j+1]时所需的row[j]已被覆盖掉,故计算新的一行时从后往前计算即可。

 vector<int> getRow(int rowIndex) {
vector<int> row(rowIndex + );
for (int i = ; i <= rowIndex; i++) {
row[] = row[i] = ;
for (int j = i - ; j > ; j--) {
row[j] = row[j - ] + row[j];
}
} return row;
}

2. 公式计算:ri = ri−1 ∗ (index − i + 1) / i

参考:http://blog.csdn.net/nomasp/article/details/50568802

 vector<int> getRow(int rowIndex) {
vector<int> row(rowIndex + );
row[] = row[rowIndex] = ;
for (int i = ; i <= (rowIndex + ) / ; i++) {
row[i] = row[rowIndex - i] = (unsigned long)(row[i - ]) * (unsigned long)(rowIndex - i + ) / i;
} return row;
}

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

  1. [LeetCode] 119. Pascal's Triangle II 杨辉三角 II

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

  2. [LeetCode] 119. Pascal's Triangle II 杨辉三角之二

    Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note t ...

  3. LeetCode 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, ...

  4. leetcode 119 Pascal's Triangle II ----- java

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

  5. Java [Leetcode 119]Pascal's Triangle II

    题目描述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return ...

  6. C#解leetcode:119. Pascal's Triangle II

    题目是: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return  ...

  7. Java for LeetCode 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 ...

  8. Leetcode 119 Pascal's Triangle II 数论递推

    杨辉三角,这次要输出第rowIndex行 用滚动数组t进行递推 t[(i+1)%2][j] = t[i%2][j] + t[i%2][j - 1]; class Solution { public: ...

  9. leetcode 118. Pascal's Triangle 、119. Pascal's Triangle II 、120. Triangle

    118. Pascal's Triangle 第一种解法:比较麻烦 https://leetcode.com/problems/pascals-triangle/discuss/166279/cpp- ...

随机推荐

  1. scalatest的userguide

    http://www.scalatest.org/user_guide 感觉功能很强大.这门语言有前途.

  2. C#进阶系列——MEF实现设计上的“松耦合”(二)

    前言:前篇 C#进阶系列——MEF实现设计上的“松耦合”(一) 介绍了下MEF的基础用法,让我们对MEF有了一个抽象的认识.当然MEF的用法可能不限于此,比如MEF的目录服务.目录筛选.重组部件等高级 ...

  3. oracle 32位导64位

    oracle 32位导64位 SHUTDOWN IMMEDIATE; STARTUP MOUNT; ALTER SYSTEM ENABLE RESTRICTED SESSION; ; ; ALTER ...

  4. 解决 PHPExcel 长数字串显示为科学计数

    解决 PHPExcel 长数字串显示为科学计数 在excel中如果在一个默认的格中输入或复制超长数字字符串,它会显示为科学计算法,例如身份证号码,解决方法是把表格设置文本格式或在输入前加一个单引号. ...

  5. Azure SQL Data Warehouse

    Azure SQL Data Warehouse & AWS Redshift Amazon Redshift Amazon Redshift 是一种快速.完全托管的 PB 级数据仓库,可方便 ...

  6. C#-WinForm-用户控件如何获取父级窗体

    1:在父窗体中定义需要在用户控件中用到的控件属性,比如,我要修改一个textbox控件.页面定义这个textbox的属性是:protected System.Web.UI.WebControls.Te ...

  7. 20.Python笔记之SqlAlchemy使用

    Date:2016-03-27 Title:20.Python笔记之SqlAlchemy使用 Tags:python Category:Python 作者:刘耀 博客:www.liuyao.me 一. ...

  8. js判断浏览器类型以及浏览器版本

    判断浏览器类型:   if navigator.userAgent.indexOf(”MSIE”)>0) {} //判断是否IE浏览器 if(isFirefox=navigator.userAg ...

  9. 仿QQ大战—服务器的搭建(ServerSocket)

    ServerSocket(服务器): ServerSocket是JAVA中提供的用于建立服务器的类: 在客户/服务器通信模式中, 服务器端需要创建监听端口的 ServerSocket, ServerS ...

  10. ReactNative 当前url和cookies的获取

    前面大概介绍了react-native的运行helloword级别的入门,所以之后简单的东西就不写了,毕竟官网上都能够找到. reactnative官网:https://facebook.github ...