题目:

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 IDE

    在Google上根据关键字"程序员鄙视链"搜索,会得到68多万条结果. 玲琅满目的搜索结果里是众多不同维度划分的鄙视链. 其中有一个维度,就是编程工具的鄙视链,比如: 而我在SAP ...

  2. 谷歌浏览器模拟手机浏览器且指定IP运行

    1.背景 因为现在项目是要做分布式,而以前使用谷歌浏览器模拟手机运行做的分布式,是指定在某台机器运行是通过Jenkins配置,来指定服务器,但是这样有一个问题,如果大家都同时配置到某台电脑,那台服务器 ...

  3. js实现弹窗一个ip在24小时只弹出一次的代码

    function cookieGO(name) { var today = new Date(); var expires = new Date(); expires.setTime(today.ge ...

  4. HTML5<fieldset>标签

    1.<fieldset>标签对表单中的相关元素进行分组. 2.<fieldset>标签会在相关表单元素周围绘制边框. <!DOCTYPE html><html ...

  5. Mybatis学习的一些细节

    一.mybatis 基本配置 最近几天一直在学习mybatis,看了一些源码,本文讲述mybatis的一些基本配置和基本的用法和注意到一些细节.个人时间和精力有限,本文属于流水账类型,不成体系,算是自 ...

  6. Java学习过程中的收获

    1. String <--> Date 这种转换要用到java.text.SimpleDateFormat类 字符串转换成日期类型: 方法1: 也是最简单的方法 Date date=new ...

  7. 修改第三方库内容,carsh提示"image not found"

    在图示位置把提示的东西加上即可 参考: iOS app with framework crashed on device, dyld: Library not loaded, Xcode 6 Beta ...

  8. Python 创建项目、应用

    1.创建项目 django-admin startproject TestPython 2.创建应用 python3 manage.py startapp books 3.目录讲解 ├── TestP ...

  9. 【例题收藏】◇例题·IV◇ Wooden Sticks

    ◇例题·IV◇ Wooden Sticks 借鉴了一下 Candy? 大佬的思路 +传送门+ (=^-ω-^=) 来源:+POJ 1065+ ◆ 题目大意 有n个木棍以及一台处理木棍的机器.第i个木棍 ...

  10. array_unique() - 去除数组中重复的元素值

      array_unique() 定义和用法 array_unique() 函数移除数组中的重复的值,并返回结果数组. 当几个数组元素的值相等时,只保留第一个元素,其他的元素被删除. 返回的数组中键名 ...