LEETCODE 118
来源:互联网 发布:实验四 数据库安全管理 编辑:程序博客网 时间:2024/04/30 14:37
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]]
第一种思路,注意到第一行第二行和其他行的生成条件不同,第一行和第二行直接全赋值为1.
其他的第i行(i>=0),有
a[i][0] = 1,a[i][i] = 1;(1)
a[i][j] = a[i-1][j-1]+a[i-1][j];
class Solution {public: vector<vector<int>> generate(int numRows) { vector<vector<int>> fin;for (int i = 0; i < numRows ; ++i){vector<int> temp;if (i > 1){temp.push_back(1);for (int j = 1; j < i; ++j)temp.push_back(fin[i-1][j-1] + fin[i-1][j]);temp.push_back(1);}elsefor (int j = 0; j <= i; j++)temp.push_back(1);fin.push_back(temp);}return fin; }};另一种思路:
不管第0行和第一行,直接将每一行的第一个和最后一个置1,其他的按照规则2:
class Solution { public: vector<vector<int> > generate(int numRows) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<vector<int> > res; for(int i = 1; i<= numRows;++i){ vector<int> row(i); row[0] = 1; // row[i-1] = 1; // for(int j = 1; j< i-1; ++j){ row[j] = res[i-2][j-1] + res[i-2][j]; // } res.push_back(row); } return res; } };
0 0
- leetcode 118
- LEETCODE 118
- leetcode 118
- leetcode 118: Partition List
- Leetcode[118] Pascal
- leetcode #118 in cpp
- leetCode练习(118)
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode
- leetcode:
- leetcode:
- LeetCode
- leetcode
- LEETCODE
- leetcode
- service
- ApplicationContextAware获取上下文bean
- NETSH WINSOCK RESET CATALOG
- RedHat Enterprise Linux 6.4使用Centos 6 的yum源
- JavaEE与Tomcat发布网页
- LEETCODE 118
- 游戏服务器:到底使用UDP还是TCP
- iOS安装CocoaPods的终端命令
- java 使用jdbc向mysql数据库中插入1亿条数据
- 向文本文件中写入内容
- spring mvc 避免IE执行AJAX时,返回JSON出现下载文件
- 欧拉项目第六题 Sum square difference
- 第二周上机实践项目 项目1-宣告”主权“
- selenium启动火狐自动加载插件的方法