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. 公式计算:r= ri1 ∗ (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. 机器学习&数据挖掘笔记_16(常见面试之机器学习算法思想简单梳理)

    前言: 找工作时(IT行业),除了常见的软件开发以外,机器学习岗位也可以当作是一个选择,不少计算机方向的研究生都会接触这个,如果你的研究方向是机器学习/数据挖掘之类,且又对其非常感兴趣的话,可以考虑考 ...

  2. Win10激活KMS

    Windows 10是目前微软主推的Windows系统,Win10 相对之前的Win7/8是吸取二者之长,发展而来! 目前新装的Windows10默认没有激活的 芝麻开们:http://pan.bai ...

  3. Git和Github简单教程

    原文链接:Git和Github简单教程 网络上关于Git和GitHub的教程不少,但是这些教程有的命令太少不够用,有的命令太多,使得初期学习的时候需要额外花不少时间在一些当前用不到的命令上. 这篇文章 ...

  4. 每个程序员都会的35个jQuery小技巧!

    1. 禁止右键点击$(document).ready(function(){ $(document).bind("contextmenu",function(e){ return ...

  5. C++链表

    之前用C写链表的时候,结点使用结构体封装的,操作起来总是感觉很麻烦.C++中使用类来封装结点,感觉操作起来方便多了,内存回收也感觉很清楚. 最近,做Gps数据分析时,别人要求加一个树形控件. Gps数 ...

  6. 解决:error: .repo/manifests/: contains uncommitted changes

    repo sync同步时提示出错:          error: .repo/manifests/: contains uncommitted changes 解决方法: 1.cd 进入.repo/ ...

  7. c#使用正则表达式抓取a标签的链接和innerhtml

    //读取网页html string text = File.ReadAllText(Environment.CurrentDirectory + "//test.txt", Enc ...

  8. Alpha阶段第三次Scrum Meeting

    情况简述 Alpha阶段第三次Scrum Meeting 敏捷开发起始时间 2016/10/24 00:00 敏捷开发终止时间 2016/10/25 00:00 会议基本内容摘要 总结了之前学习的内容 ...

  9. 10月30日下午 PHP精确查询(模糊查询、模糊+关键字共同查询)

    1.一个条件的模糊查询 <body> <br /> <form action="main.php" method="post"&g ...

  10. mysql常用操作语句

    mysql常用操作语句 1.mysql -u root -p   2.mysql -h localhost -u root -p database_name 2.列出数据库: 1.show datab ...