题目:

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. [转载]在VB.Net中获取COM对象的特定实例(Getting a specific instance of COM object in VB.Net)

    转载:http://www.it1352.com/534235.html 问题: I am writing a Windows Form application in .Net to list all ...

  2. May 03rd 2017 Week 18th Wednesday

    Truth needs no colour; beauty, no pencil. 真理不需要色彩,美丽不需要涂饰. There is no absoulte truth and everlastin ...

  3. 2018.9.9 Tomcat是怎样运行的

    一. Servlet容器是怎样工作的 一个Servlet容器是一个复杂的系统.然而,对于处理对Servlet的请求,Servlet容器主要做三件事情: 1. 创建请求对象,并设置所调用的Servlet ...

  4. Java的感受

    感觉Java很重要,但是学起来好像并不比C语言简单.

  5. P1266 速度限制

    P1266 速度限制 第一次接触这种分层spfa 类似于dp 个人理解 #include<cstdio> #include<iostream> #include<algo ...

  6. BIO与NIO

    BIO与NIO 1.传统BIO (1)特点 面向数据流 阻塞式传输 一个客户端对应一个线程 在客户机增多的情况下,线程资源随之增多,会造成cpu资源枯竭 (2)需求 ​ 客户机向服务器输出字符串,逐一 ...

  7. react事件绑定的三种常见方式以及解决Cannot update during an existing state transition (such as within `render`). Render methods should be a pure function of props and state问题思路

    在 React 组件中,每个方法的上下文都会指向该组件的实例,即自动绑定 this 为当前组件. 而且 React 还会对这种引用进行缓存,以达到 CPU 和内存的优化.在使用 ES6 classes ...

  8. Oracle字符集的查看查询和Oracle字符集的设置修改(转载)

    本文主要讨论以下几个部分:如何查看查询oracle字符集. 修改设置字符集以及常见的Oracle UTF8字符集和Oracle exp 字符集问题. 一.什么是Oracle字符集 Oracle字符集是 ...

  9. html编写头部,mata的含义

    <meta name="viewport" content="width=device-width, initial-scale=1.0"> con ...

  10. ethereum(以太坊)(一)

    从这周开始,开始学习以太坊开发--solidity,开始决定往区块链方向发展,毕竟区块链技术应用广泛.一开始接触solidity开发语言不太习惯,毕竟一直在学习python语法,有很多都不能接受.有难 ...