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. jQuery基本API小结(下)---工具函数-基本插件

    一.工具函数 1.获取浏览器的名称与版本信息 在jQuery中,通过$.browser对象可以获取浏览器的名称和版本信息,如$.browser.chrome为true,表示当前为Chrome浏览器,$ ...

  2. java设计模式之责任链模式(Chain of Responsibility)

    转自:http://www.cnblogs.com/java-my-life/archive/2012/05/28/2516865.html 在阎宏博士的<JAVA与模式>一书中开头是这样 ...

  3. 有关C#中的引用类型的内存问题

    对于一个类,如果定义后(记作对象a),将另外一个对象b直接赋值(“a = b”)给它,则相当于将地址赋值给了这个对象.当另外一个对象b不再对这块地址应用时,a由于对这块地址仍在使用,这块地址的指向的栈 ...

  4. 转载--js对象无法当成参数传递

    今天我碰到了这个问题一头雾水,明明记得对象是可以传参的啊.我使用了一款基于bootstrap的表格插件DataTables,想把行信息直接传给操作函数,方便编辑(此行信息是一个对象,按道理可以的啊), ...

  5. JAVA用email.jar发送邮件

    1 jar包 email.jar包,网上下载 2 源代码 package zjr.amy.emil.test; import java.util.Date; import java.util.Prop ...

  6. Spring <context:annotation-config />讲解

    在基于主机方式配置Spring的配置文件中,你可能会见到<context:annotation-config />这样一条配置,他的作用是向Spring容器注册AutowiredAnnot ...

  7. Apache Hive (二)Hive安装

    转自:https://www.cnblogs.com/qingyunzong/p/8708057.html Hive的下载 下载地址http://mirrors.hust.edu.cn/apache/ ...

  8. 【UVA11212 算法竞赛入门经典】 Editing a Book 【IDA*】

    题意 你有一篇由n(2<=n<=9)个自然段组成的文章,希望将它们排列成1,2,···,n.可以用剪切和粘贴来完成任务.每次可以剪切一段连续的自然段,粘贴时按照顺序粘贴.注意剪贴板只有一个 ...

  9. 129. Sum Root to Leaf Numbers(Tree; DFS)

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  10. 钉钉开发笔记(六)使用Google浏览器做真机页面调试

    注: 参考文献:https://developers.google.com/web/ 部分字段为翻译文献,水平有限,如有错误敬请指正 步骤1: 从Windows,Mac或Linux计算机远程调试And ...