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 ...
随机推荐
- 【主席树】bzoj1112: [POI2008]砖块Klo
数据结构划一下水 Description N柱砖,希望有连续K柱的高度是一样的. 你可以选择以下两个动作 1:从某柱砖的顶端拿一块砖出来,丢掉不要了. 2:从仓库中拿出一块砖,放到另一柱.仓库无限大. ...
- 关于.ascx
.ascx用户控件 参考系列教程User controls in asp.net - Part 104 用户控件包含了html.代码和其他Web或者用户控件的组合,并在Web服务器上以自己的文件格式 ...
- Cacti安装脚本Server端+客户端
#!/bin/bash #auto make install LAMP+Cacti #by authors zhang #RRDtool define path variable R_FILES=rr ...
- Django two
http://www.cnblogs.com/yuanchenqi/articles/6083427.html Django: 1.安装Django pip install django 2.创建p ...
- Python学习-day10 进程
学习完线程,学习进程 进程和线程的语法有很多一样的地方,不过在操作系统中的差别确实很大. 模块是threading 和 multiprocessing 多进程multiprocessing multi ...
- [python测试框架学习篇] 分享一个和adb相关的测试框架
https://testerhome.com/topics/7106 (user: zteandallwinner password: same to qq ) 264768502 · # ...
- rsync配置和同步数据
rsync的搭建配置1.环境和配置文件 rsyncd.conf(主配置文件) rsyncd.secrets(密码文件) pc1:192.168.0.1,rsync的服务器,配置rsyncd.conf文 ...
- 利用Python从文件中读取字符串(解决乱码问题)
首先声明这篇学习记录是基于python3的. python3中,py文件中默认的文件编码就是unicode,不用像python2中那样加u,比如u'中文'. 不过在涉及路径时,比如C:\Users\A ...
- 【bzoj1449/bzoj2895】[JSOI2009]球队收益/球队预算 费用流
题目描述 输入 输出 一个整数表示联盟里所有球队收益之和的最小值. 样例输入 3 3 1 0 2 1 1 1 10 1 0 1 3 3 1 2 2 3 3 1 样例输出 43 题解 费用流 由于存在一 ...
- 【Luogu】P1472奶牛家谱(DP)
题目链接 这是一道考思维的好题. 一开始设f[i][j]是i个点刚好j层的方案数,死活调不出来,看题解发现可以改为<=j层的方案数,最后输出f[n][m]-f[n][m-1]就好了. 对于计算考 ...