leetcode笔记:Pascal's Triangle
一. 题目描写叙述
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]
]
二. 题目分析
关于帕斯卡三角形的定义,可參考:http://baike.baidu.com/link?url=qk_-urYQnO4v6v3P4BuMtCa0tMNUqJUk4lmbkb1aqbqikBU-ndiMlTF20fq2QUjTTFTeTohZ72KFxgBnz4sJha
将该三角形的左边对齐。就行发现,tri[i][j] = tri[i-1][j-1] + tri[i-1][j]
。因此这里的代码并没有什么技巧。
三. 演示样例代码
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> result;
if (numRows <= 0)
return result;
else
{
vector<int> temp(1, 1);
result.push_back(temp);
if (numRows == 1) return result;
temp.push_back(1);
result.push_back(temp);
if (numRows == 2) return result;
for (int i = 2; i < numRows; ++i)
{
vector<int> temp(i + 1, 1);
for (int j = 1; j < i; ++j)
temp[j] = result[i - 1][j - 1] + result[i - 1][j];
result.push_back(temp);
}
}
return result;
}
};
leetcode笔记:Pascal's Triangle的更多相关文章
- 【Leetcode】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 ...
- leetcode:Pascal's Triangle
一. 题目 经典题目,杨辉三角,输入行数.生成杨辉三角的数组. 二. 分析 首先,我们知道有例如以下规律: 1.每一行的第一个数和最后一个数都为1 2.中间的数是上面数和上面数左边的数 ...
- LeetCode——Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- 【leetcode】118. Pascal's Triangle
@requires_authorization @author johnsondu @create_time 2015.7.23 19:54 @url [Pascal's Triangle](http ...
- LeetCode——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 ...
- leetcode - Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- [LeetCode]Pascal's Triangle II
题目:给定一个行索引index,返回帕斯卡三角形第index层的三角形 算法:生成index层帕斯卡三角形,并返回第index层三角形 public class Solution { public L ...
- Pascal's Triangle I,II
题目来自于Leetcode https://leetcode.com/problems/pascals-triangle/ Given numRows, generate the first numR ...
- 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 ...
随机推荐
- Working out (DP)
Summer is coming! It's time for Iahub and Iahubina to work out, as they both want to look hot at the ...
- 九度oj 题目1017:还是畅通工程
题目描述: 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可 ...
- POJ 2092 Grandpa is Famous
Grandpa is Famous Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 7153 Accepted: 3624 ...
- RAC+单节点搭建DG
primary RAC to single standby 参考文献:RAC+单实例DATAGUARD 配置 http://blog.csdn.net/miyatang/article/detai ...
- MATLAB(1)
前言 之前经常用MATLAB,却不小心停留在了舒适区,连基本的调试方法都没有掌握.本文主要是对MATLAB程序调试中的一般方法进行总结,也是自己学习的记录.全文大致分为三个段落: 1)代码内调试: 2 ...
- leetcode 15. 3Sum 二维vector
传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...
- iOS 设置RGB色的宏
转自:http://lizhuang.iteye.com/blog/1931768 //RGB Color macro #define UIColorFromRGB(rgbValue) [UICol ...
- 1861 奶牛的数字游戏 2006年USACO
codevs——1861 奶牛的数字游戏 2006年USACO 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 青铜 Bronze 题解 题目描述 Descript ...
- python3.x之print()
1.print内容 #!/usr/bin/python print('hello world') //print("hello world") 2.print变量 #!/us ...
- luogu P2912 [USACO08OCT]牧场散步Pasture Walking
题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures ...