LeetCode OJ——Pascal's Triangle
http://oj.leetcode.com/problems/pascals-triangle/ 杨辉三角
先分析数据,找出规律
ans[row][col] = ans[row-1][col-1]+ans[row-1][col]
#include <iostream>
#include <vector>
#include <string>
using namespace std; class Solution {
public:
vector<vector<int> > generate(int numRows) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
vector<vector<int> > ans;
ans.clear();
if(numRows<=)
return ans;
//
vector<int> onepiece;
onepiece.push_back();
ans.push_back(onepiece);
if(numRows == )
return ans;
//
onepiece.clear();
onepiece.push_back();
onepiece.push_back();
ans.push_back(onepiece);
if(numRows == )
return ans; //3...
for(int row = ;row < numRows;row++)
{
onepiece.clear();
for(int col = ;col <= row;col++)
{
if(col == || row == col)
onepiece.push_back();
else
onepiece.push_back(ans[row-][col-]+ans[row-][col]);
}
ans.push_back(onepiece);
}
return ans;
}
}; int main()
{
Solution mysolution;
mysolution.generate(); return ;
}
LeetCode OJ——Pascal's Triangle的更多相关文章
- LeetCode OJ——Pascal's Triangle II
http://oj.leetcode.com/problems/pascals-triangle-ii/ 杨辉三角2,比杨辉三角要求的空间上限制Could you optimize your algo ...
- LeetCode 118 Pascal's Triangle
Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- [LeetCode] 119. Pascal's Triangle II 杨辉三角 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 II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- [LeetCode] 119. Pascal's Triangle II 杨辉三角之二
Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note t ...
- LeetCode 119. 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 118. Pascal's Triangle (杨辉三角)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- leetcode 【 Pascal's Triangle II 】python 实现
题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [ ...
- leetcode 【 Pascal's Triangle 】python 实现
题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,R ...
随机推荐
- 如何用 CSS 和 D3 创作旋臂粒子动画
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/xJrOqd 可交互视频 ...
- destoon 屏蔽会员组,让个人,游客不显示
include/post.fun.php 文件的group_select函数增加 排除参数 except function group_select($name = 'groupid', $titl ...
- destoon 信息发布表单提交验证
sell 模块的form表单如下: <form method="post" id="dform" action="?" target= ...
- 解决oh-my-zsh卡顿问题
git config --global oh-my-zsh.hide-status 1
- 牛客练习赛22 C 简单瞎搞题
//位运算 // & 都是1 才是 1 // | 都是0 才是0 // ^ 不一样才是1 #include <iostream> #include <cstdio> # ...
- centos配置jdk
########## config jdk ########## export JAVA_HOME=/usr/local/java/jdk1.7.0_79 export CLASSPATH=.:${J ...
- Linux优化总结
1)netstat (*****)查看网络状态lntup或an 1.[listening|-l] 2.[--numeric|-n] 3.[--tcp|-t] 4.[--udp|-u] 5[--prog ...
- 使用fio测试磁盘I/O性能
简介: fio是测试IOPS的非常好的工具,用来对硬件进行压力测试和验证,支持13种不同的I/O引擎,包括:sync,mmap, libaio, posixaio, SG v3, splice, nu ...
- django_orm操作
查询操作和性能优化 1.基本操作 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 增 models.Tb1.object ...
- 遍历Request.QueryString
Request.QueryString 返回的是 NameValueCollection, 而NameValueCollection实现了IEnumerable的GetEnumerator方法,只是G ...