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. linux 之常见的好用命令

    参考网址:软件匠艺小组之第八期把命令行玩飞起来 1.如果想要将文件重定向到文件里,而又想看重定向的内容, tee命令 例如:ls | tee foot.txt 2.如果想要字母显示为大写独特的,命令: ...

  2. HDU 1232 并查集/dfs

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...

  3. CSS伪选择器的使用-遁地龙卷风

    分为伪元素选择器和伪类选择器两种,前者两个冒号,后者一个冒号,但是浏览器都看做一个冒号 1.a.::first-line 逐层匹配,直到有文本元素且结束改行为止 设置css属性word-break:b ...

  4. JAVA多线程基础知识(一)

    一. 基础知识 要了解多线程首先要知道一些必要的概念,如进程,线程等等.开发多线程的程序有利于充分的利用系统资源(CPU资源),使你的程序执行的更快,响应更及时. 1. 进程,一般是指程序或者任务的执 ...

  5. 使用Monitor调试Unity3D Android程序日志输出(非DDMS和ADB)

    使用Monitor调试Unity3D Android程序日志输出(非DDMS和ADB) http://www.cnblogs.com/mrkelly/p/4015245.html 以往调试Androi ...

  6. 使用MyEclipse生成Java注释时,使用的Code Template

    设置注释模板的入口: Window->Preference->Java->Code Style->Code Template, 然后展开Comments节点就是所有需设置注释的 ...

  7. VMware vCenter Server安装图解教程

    安装说明: 1.安装VMware vCenter Server的主机操作系统为:Windows Server 2008 R2 2.在Windows Server 2008 R2中需要预先安装好SQL ...

  8. Java计算程序运行时间

    public static void main(String[] args) { // TODO Auto-generated method stub long nd = 1000 * 24 * 60 ...

  9. typedef使用大全(转)

    typedef使用大全(转) 一.数组typedef到处都是,但是能够真正懂得typedef使用的不算太多.对于初学者而言,看别人的源码时对到处充斥的typedef往往不知所错,而参考书又很少,所以在 ...

  10. js判断checkbox状态,处理表单提交事件

    功能描述:手机网页需要一个投票功能,通过form的post提交.有5-20个checkbox选项,至少选一项,至多选三项.需要在用户点击提交按钮前,判断checkbox的状态是否符合条件,符合则提交到 ...