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的更多相关文章

  1. LeetCode OJ——Pascal's Triangle II

    http://oj.leetcode.com/problems/pascals-triangle-ii/ 杨辉三角2,比杨辉三角要求的空间上限制Could you optimize your algo ...

  2. LeetCode 118 Pascal's Triangle

    Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows  ...

  3. [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, ...

  4. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

  5. [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 ...

  6. 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, ...

  7. LeetCode 118. Pascal's Triangle (杨辉三角)

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  8. 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 [ ...

  9. leetcode 【 Pascal's Triangle 】python 实现

    题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,R ...

随机推荐

  1. numpy学习(一)

    numpy数据类型 # numpy创建对象,numpy创建的对象是n阶矩阵,类似python中列表的嵌套 nd = np.array([[1,2,3,4,5],[2,3,4,6,5]])nd 结果: ...

  2. MySQL数据库---索引

    索引的作用就是快速找出在一个列上用一特定值的行.如果没有索引,MySQL不得不首先以第一条记录开始并然后读完整个表直到它找出相关的行. 索引的类型: 先写一个建表语句: CREATE TABLE `t ...

  3. 标准C++(1)

    一.引用 引用就是某一变量(目标)的一个别名,对引用的操作与对变量直接操作完全一样. 引用的声明方法:类型标识符 &引用名=目标变量名: 例: int& num; 引用类似于起别名 注 ...

  4. LeetCode(224) Basic Calculator

    题目 Implement a basic calculator to evaluate a simple expression string. The expression string may co ...

  5. HDU - 1251 统计难题(Trie树)

    有很多单词(只有小写字母组成,不会有重复的单词出现) 要统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). 每个单词长度不会超过10. Trie树的模板题.这个题内存把控不好容易MLE. ...

  6. jenkins的构建项目配置

    继http://www.cnblogs.com/yajing-zh/p/5109517.html搭建好jenkins系统配置之后,新建jenkins构建项目,用于自动化构建. 点击Jenkins界面左 ...

  7. Python属性描述符(一)

    描述符是对多个属性运用相同存取逻辑的一种方式,,是实现了特性协议的类,这个协议包括了__get__.__set__和__delete__方法.property类实现了完整的描述符协议.通常,可以只实现 ...

  8. bootstrap 弹出框(Popover)插件 修改title等属性选项值

    <button type="button" class="btn btn-default ht-btn" data-toggle="popove ...

  9. Apache 根据不同的端口 映射不同的站点

    以前,在本地新建个项目,总是在Apache的htdocs目录下新建个项目目录,今年弄了个别人写好的网站源码,因为该系统的作者假定网站是放在根目录的,放在二级目录下会出错.所以无奈,只能想办法,根据端口 ...

  10. [POJ 1008] Maya Calendar C++解题

        Maya Calendar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 62297   Accepted: 192 ...