题目:

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. 解决使用phpmyadmin导出导入数据库时提示的“超出长度”、“超时”问题

    IIS请求筛选模块被配置为拒绝超过请求内容长度的请求 1. 修改IIS的applicationhost.config a.文件位置: %windir%/system32/inetsrv/config/ ...

  2. leetcode: 贪心

    1. jump game Given an array of non-negative integers, you are initially positioned at the first inde ...

  3. 会说话的ABAP report

    report z. INCLUDE ole2incl. DATA: ole   TYPE ole2_object,       voice TYPE ole2_object,       text   ...

  4. 153. Find Minimum in Rotated Sorted Array(leetcode, binary search)

    https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/ leetcode 的题目,binary ...

  5. 轻量级HTTP服务器Nginx(入门与安装篇)

    轻量级HTTP服务器Nginx(入门篇)   文章来源于南非蚂蚁   一.什么是Nginx 相信很多读者都对Apache非常熟悉,与Apache类似,Nginx是一款高性能的HTTP和反向代理服务器软 ...

  6. Task 的入门

    https://www.cnblogs.com/huangxincheng/archive/2012/04/03/2430638.html

  7. l1,l2norm

    http://www.chioka.in/differences-between-l1-and-l2-as-loss-function-and-regularization/ 这里分别对l1 loss ...

  8. pod 指令无效

    到了新公司,配置pod,死活找不到pod指令,用了很多方法之后,找到了解决办法 sudo vim .bash_profile 然后添加 export PATH=/usr/local/bin:$PATH ...

  9. vue学习之路 - 0.背景

    1 单页面应用程序 Single Page Application (SPA) 从字面意义来看就是一个网站就一个页面,如: coding 网易云音乐 极致的用户体验,就像nativeapp一样 优点: ...

  10. java后台输入数据的2种方式

    java后台输入数据的2种方式 (1) import java.io.BufferedReader; import java.io.InputStreamReader; public class 输入 ...