【leetcode】Pascal's Triangle I & II (middle)
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)的更多相关文章
- 【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 ...
- 【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 ...
- 【leetcode】Bitwise AND of Numbers Range(middle)
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- 【leetcode】Longest Substring Without Repeating Characters (middle)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- 【LeetCode】385. Mini Parser 解题报告(Python)
[LeetCode]385. Mini Parser 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/mini-parser/ ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】911. Online Election 解题报告(Python)
[LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
随机推荐
- 基于iSCSI的SQL Server 2012群集测试(四)--模拟群集故障转移
6.模拟群集故障转移 6.1 模拟手动故障转移(1+1) 模拟手动故障转移的目的有以下几点: 测试群集是否能正常故障转移 测试修改端口是否能同步到备节点 测试禁用full-text和Browser服务 ...
- 网页兼容浏览器测试工具Multibrowser
网页兼容性测试工具(MultiBrowser),有firefox,chrome,IE 下载
- 绝不要进行两层间接非const指针赋值给const指针
#include <stdio.h> #include <stdlib.h> int main(void) { int *p1; int * *pp1; const int * ...
- css固定元素位置(fixed)
来源:http://www.cnblogs.com/lecaf/archive/2011/03/25/fixed.html fixed是一种特殊的absolute,同样不占文档流,特殊的地方在于fix ...
- android Activity基类通用方法
public class BaseActivity extends Activity { Resources res; // 通用资源缩写 @Override protected void onCre ...
- 瀑布流js排列
var _px = document.getElementById("px"); var con=document.getElementById("content&quo ...
- 微型Http服务器Tiny Http Server
Tiny Http Server 一个简单的跨平台Http服务器.服务器部分使用了Mongoose的代码,界面是使用QT开发的. 开发为了在临时需要使用一个http服务器来做发布代码文档的时候,不用去 ...
- 把ZenCart在线商店搭建到本地
前些日子,要给在线的zencart商店做一些小改动,需要安装一个插件.大家都知道,zencart有很多插件选用,兼容性也好坏不一,直接在正在运营的网站程序上修改,难免会影响到客户体验,出什么差错了代价 ...
- c++ SOA Axis2c 编译安装
Axis2C 安装过程 1设置环境变量 export AXIS2C_HOME=/usr/local/axis2c 2.下载源码包解压编译安装 cd axis2c-src-1.6.0 ./configu ...
- phalcon 前端代码结构
phalcon 前端举例: (1) baisic.phtml + basic_ajax_get.phtml + basic_ajax_post.phtml (2) basic_get.phtml ...