Given numRows, generate the first numRows of Pascal's triangle.

For example, given numRows = 5,
Return

[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]

思路:杨辉三角,直接按规律生成即可

vector<vector<int> > generate(int numRows) {
vector<vector<int>> ans;
for(int i = ; i < numRows; i++)
{
vector<int> v(i + , );
for(int j = ; j < i; j++)
{
v[j] = ans[i - ][j - ] + ans[i - ][j];
}
ans.push_back(v);
}
return ans;
}

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?

思路:

要靠数学公式了,设杨辉三角的最顶层为第0行,每行的第一个数字是第0个,则

第 i 行第 j 个元素的计算为:C(i, j) =  (i)! / (j)! * (i - j)!

那么第 i 行第 j 个元素和它前一个元素的关系是  ans[i][j] = ans[i][j - 1] * (i - j + 1) / j; //这里要注意不要越界

vector<int> getRow(int rowIndex) {
vector<int> ans(rowIndex + , );
ans[rowIndex - ] = ans[] = rowIndex;
for(int i = ; i < rowIndex / + ; i++)
{
ans[rowIndex - i] = ans[i] = (long long)ans[i - ] * (rowIndex - i + ) / i; //用long long防止越界 同时利用杨辉三角的对称性减少一半的计算量
}
return ans;
}

【leetcode】Pascal's Triangle I & II (middle)的更多相关文章

  1. 【leetcode】House Robber & House Robber II(middle)

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  2. 【leetcode】Binary Tree Right Side View(middle)

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  3. 【leetcode】Bitwise AND of Numbers Range(middle)

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

  4. 【leetcode】Longest Substring Without Repeating Characters (middle)

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  5. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

  6. 【LeetCode】385. Mini Parser 解题报告(Python)

    [LeetCode]385. Mini Parser 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/mini-parser/ ...

  7. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  8. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  9. 【LeetCode】911. Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

随机推荐

  1. [译]git fetch

    git fetch从远程仓储导入commit到你的本地仓储. 这些fetch到的commit是做为一个远程分支存储在你本地的. 这样你可以在集成这些commit到你的项目前先看看都有些什么修改. 用法 ...

  2. iOS:使用MVC模式帮ViewController瘦身

    如何给UIViewController瘦身 随着程序逻辑复杂度的提高,你是否也发现了App中一些ViewController的代码行数急剧增多,达到了2,3千行,甚至更多.这时如果想再添加一点功能或者 ...

  3. putchar和puts

    #include<stdio.h> int main() { char a = 'h'; char b[] = "hello"; putchar(a); //putch ...

  4. MVC后台传dt数据

    //MVC后台传dt数据 public JsonResult TeacherVoteInfo([FromBody]Teacher_VoteModel model) { string tname = m ...

  5. Android三种消息提示

    Android消息提示有三种方式: 1  使用Toast显示消息提示框 Toast类用于在屏幕中显示一个提示信息框,该消息提示框没有任何控制按钮,并且不会获得焦点,经过一定时间后自动消失.通常用于显示 ...

  6. JQuery data方法的使用-遁地龙卷风

    (-1)说明 我用的是chrome49,这个方法涉及到JQuery版本问题,我手里有3.0的,有1.9.1,后面将1.9.1及其以前的称为低版本,3.0称为高版本 测试例子用到的showMessage ...

  7. Android学习之路书籍推荐

    Android开发书籍推荐:从入门到精通系列学习路线书籍介绍 JAVA入门书籍: < Introduction to java programming > < Core java & ...

  8. 转:linux下bwa和samtools的安装与使用

    bwa的安装流程安装本软体总共需要完成以下两个软体的安装工作:1) BWA2) Samtools1.BWA的安装a.下载BWA (download from BWA Source Forge ) ht ...

  9. CentOS 7更换 安装源

    centos 安转源更换 一:切换到目录 cd /etc/yum.repos.d二: [备份] mv CentOS-Base.repo  CentOS-Base.repo.bak三 : [下载] wg ...

  10. php在centos下的脚本没有解析的问题

    如题,参考了许多,比如:http://serverfault.com/questions/523131/php5-is-installed-but-apache-is-displaying-php-a ...