题目:

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<vector<int> > ret;
if ( numRows< ) return ret;
vector<int> pre, curr;
for ( int i=; i<numRows; ++i )
{
curr.clear();
curr.push_back();
for ( int j=; j<pre.size(); ++j )
{
if ( j==pre.size()- ){
curr.push_back();
}
else{
curr.push_back(pre[j]+pre[j+]);
}
}
pre = curr;
ret.push_back(curr);
}
return ret;
}
};

tips:

数组基本操作。

==============================================

第二次过这道题,代码更简洁了。

class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int> > ret;
if ( numRows< ) return ret;
vector<int> pre;
pre.push_back();
ret.push_back(pre);
for ( int i=; i<numRows; ++i )
{
pre = ret.back();
vector<int> curr;
curr.push_back();
for ( int j=; j<pre.size(); ++j ) curr.push_back(pre[j-]+pre[j]);
curr.push_back();
ret.push_back(curr);
}
return ret;
}
};

===========================================

第三版,更简洁了一些

class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int> > ret;
if (numRows==) return ret;
vector<int> tmp;
tmp.push_back();
ret.push_back(tmp);
for ( int i=; i<numRows; ++i )
{
for ( int j=tmp.size(); j>; --j ) tmp[j] = tmp[j] + tmp[j-];
tmp.push_back();
ret.push_back(tmp);
}
return ret;
}
};

【Pascal's Triangle】cpp的更多相关文章

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

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

  2. 【Pascal's Triangle II 】cpp

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

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

  4. 【Triangle 】cpp

    题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...

  5. hdu 4740【模拟+深搜】.cpp

    题意: 给出老虎的起始点.方向和驴的起始点.方向.. 规定老虎和驴都不会走自己走过的方格,并且当没路走的时候,驴会右转,老虎会左转.. 当转了一次还没路走就会停下来.. 问他们有没有可能在某一格相遇. ...

  6. 【Search Insert Position 】cpp

    题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...

  7. 【First Missing Positive】cpp

    题目: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2 ...

  8. 【Insertion Sorted List】cpp

    题目: Sort a linked list using insertion sort. 代码: /** * Definition for singly-linked list. * struct L ...

  9. 【Merge Sorted Array】cpp

    题目: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Not ...

随机推荐

  1. SAP Fiori里的List是如何做到懒加载Lazy load的

    今天一同事问我这个问题:S/4HANA Fiori应用里的列表,一旦Scroll到底部就会自动向后台发起新的请求把更多的数据读取到前台显示. 以Product Master这个应用为例,我点击搜索之后 ...

  2. iPad游戏 Calcculator: The Game 程序自动计算求解方法

    今天在iPad上下了个小游戏,主要是一个计算器的界面,有开始值,目标值,限定步数,以及一些加减乘除,还有作者脑洞想出来的功能键,主要有左移,直接把一个数加到末尾,将其中的某个数改为另一个数等等..玩到 ...

  3. C#类的实例

    一.C#类的实例 类的实例其实就是类的对象.定义类之后即可声明类的对象,声明对象之后便可以访问对象的成员了. 语法格式如下: 类名 对象名;         // 声明对象对象名 = new 类名() ...

  4. 6.安装使用vue-quill-editor

    前言: 在vue项目中,因为涉及到使用文本编辑器, 恰恰vue-quill-editor就是一个简单实用的富文本编辑器. 参考文档:vue中使用vue-quill-editor富文本编辑器,自定义to ...

  5. iOS新浪微博OAuth2.0认证代码

    #import "ViewController.h" #import "AFNetworking.h" @interface ViewController () ...

  6. 总结ing

    1,iOS的GCD中如何关闭或者杀死一个还没执行完的后台线程? 举例来说,我通过导航进入到了一个视图,这个视图加载的时候会新建一个线程在后台运行,假设这个线程需要从网络中读取许多数据,需要一定的时间, ...

  7. PowerDesigner生成sql脚本

    1.打开PowerDesigner->New Project; 2.填写项目名称,选择文件的存放路径: 3.新建一个模型,New Model: 4.选择概念模型,填写模型名称: 5.选择enti ...

  8. angular学习之angular-phonecat项目的实现

    ---恢复内容开始--- AngularJS官方网站提供了一个用于学习的示例项目:PhoneCat.这是一个Web应用,用户可以浏览一些Android手机,了解它们的详细信息,并进行搜索和排序操作. ...

  9. Spring框架进阶3

    Spring框架进阶3 测试spring_jdbc和spring对事务的管理 先配置相应的pom <?xml version="1.0" encoding="UTF ...

  10. Shell脚本使用汇总整理

    Shell脚本使用汇总整理 一.Shell脚本常用的头部格式: 头部的作用就是告知linux此脚本的类型: 常用的头部格式如下:(/bin/bash,是bash的路径,如果不知道路径可以通过which ...