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]
] 产生杨辉三角,比较简单,注意几个边界条件:
 class Solution {
public:
vector<vector<int> > generate(int numRows) {
vector<int> vi;
vector<vector<int> > ans;
int i,j;
vi.push_back();
ans.clear(); // 注意初始化
9 if(numRows<=0) return ans;
10 if(1==numRows)
11 {
12 ans.push_back(vi);
13 return ans;
14 }
15 if(2==numRows)
16 {
17 ans.push_back(vi);
18 vi.push_back(1);
19 ans.push_back(vi);
20 return ans;
21 } ans.push_back(vi);
vi.push_back();
ans.push_back(vi);
for(i=;i<numRows;i++)
{
vi.clear();
vi.push_back();
for(j=;j<i;j++)
{
vi.push_back(ans[i-][j-]+ans[i-][j]);
}
vi.push_back();
ans.push_back(vi);
}
return ans;
}
};

转载请注明出处: http://www.cnblogs.com/double-win/ 谢谢!

[LeetCode 题解]: Pascal's Triangle的更多相关文章

  1. LeetCode 118 Pascal's Triangle

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

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

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

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

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

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

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

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

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

  8. leetcode 118. Pascal's Triangle 、119. Pascal's Triangle II 、120. Triangle

    118. Pascal's Triangle 第一种解法:比较麻烦 https://leetcode.com/problems/pascals-triangle/discuss/166279/cpp- ...

  9. 【leetcode】Pascal's Triangle II

    题目简述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Retur ...

随机推荐

  1. 说说JDK中的List-ArrayList、Vector、LinkedList

    为方便开发人员,JDK提供了一套主要数据结构的实现,比如List.Map等.今儿说说List接口. List接口的一些列实现中,最常用最重要的就是这三个:ArrayList.Vector.Linked ...

  2. PHP函数内访问全局变量

    $dbcon='123'; 方法一.funtion fun1(){global $dbcon;$dbcon-> 就可以访问了.} 方法二$GLOBALS['$dbcon'];

  3. application-defined exception

    dataSnap服务器,客户端调用的时候写错了一句话, SQLConnection1->CloneConnection(); 改为 SQLConnection1->Close(); 就好了 ...

  4. Oracle11gR2-聚簇因子浅析

    创建表t1,t2 SQL> conn n1/n1 Connected. SQL> SQL> SQL> create table t1 as select trunc(rownu ...

  5. python 协程 gevent 简单测试

    串行测试 from gevent import monkey; monkey.patch_all()#有IO才做时需要这一句 import gevent import requests,time st ...

  6. mysql update更新带子查询的实现方式

    现在要做一下数据移植,需要更新相关的数据,需要在mysql中更新时不能将更新的表作为查询的表. 总结一下: 一:单表更新时 例如: update customer set category = 1 W ...

  7. iOS开发基础控件--UITextField

    001 //初始化textfield并设置位置及大小 002   UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20 ...

  8. haproxy 配置 说明

    一.环境说明实验环境OS CentOS5.4192.168.0.14    proxy192.168.0.24    web1192.168.0.64    web2 官方地址:http://hapr ...

  9. jmeter 读取mysql数据库

    业务背景 当我们用jmeter进行压测,或者造数据的时候,我们可能希望每次请求的参数都是随机的.如果从一个文件里读取,很难达到要求.jmeter提供了一套读取数据库的组件,能满足部分要求.但性能不好, ...

  10. Sql Server 2008R2 数据库发布与订阅

    背景描述: 发布服务器A: (远程端) , 数据库服务名: GUANWANG1 订阅服务器B: (本机)   ,  数据库服务名: PC-LLRDBA 需要从服务器A中数据库发布,然后在B中订阅A发布 ...