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] ]

C++

class Solution {
public:
vector<vector<int> > generate4(int numRows) {
vector<vector<int>> dp(numRows);
if (numRows < 1) return dp;
dp[0].push_back(1);
for (int i =1; i < numRows; i++){
dp[i].push_back(1);
for(int j = 1; j < i; j++)
dp[i].push_back(dp[i-1][j-1] + dp[i-1][j]);
dp[i].push_back(1);
}
return dp;
} vector<vector<int>> generate(int numRows){
vector<vector<int>> dp(numRows);
for(int i = 0; i < numRows; ++i){
dp[i].push_back(1);
for(int j = 1; j < i; ++j)
dp[i].push_back(dp[i-1][j-1] + dp[i-1][j]);
if (i>0)
dp[i].push_back(1);
}
return dp;
}
};

pascals-triangle leetcode C++的更多相关文章

  1. [Leetcode] pascals triangle ii 帕斯卡三角

    Given an index k, return the k th row of the Pascal's triangle. For example, given k = 3,Return[1,3, ...

  2. [LeetCode][Java]Triangle@LeetCode

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  3. Triangle leetcode

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  4. Triangle——LeetCode

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  5. Pascal's Triangle leetcode

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  6. Triangle LeetCode |My solution

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  7. Pascal's Triangle leetcode java(杨辉三角)

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

  8. Triangle leetcode java

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

  9. triangle leetcode C++

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  10. 决战Leetcode: easy part(1-50)

    本博客是个人原创的针对leetcode上的problem的解法,所有solution都基本通过了leetcode的官方Judging,个别未通过的例外情况会在相应部分作特别说明. 欢迎互相交流! em ...

随机推荐

  1. 论文解读(SimCLR)《A Simple Framework for Contrastive Learning of Visual Representations》

    1 题目 <A Simple Framework for Contrastive Learning of Visual Representations> 作者: Ting Chen, Si ...

  2. scrum项目冲刺_day11 第一阶段总结

    "智能垃圾分类APP"第一阶段总结 总任务: 一.appUI页面(已完成) 二.首页功能: 1.图像识别功能(已完成) 2.语音识别功能(已完成) 3.垃圾搜索功能(基本完成) 4 ...

  3. 01-mysql必知必会

    概述 主键:唯一区分表中每个行(不可重复,不允许null mysql:基于客户-服务器的数据库系统 使用mysql mysql是部署在主机上的,可以通过ssh建立mysql连接 # 显示数据库 sho ...

  4. PHP的Sodium加密扩展函数了解

    这是本次加密扩展系列的最后一篇文章,也是我们要学习了解的最后一个 PHP 加密扩展.Sodium 出现的目的也是为了代替 Mcrypt 这个原来的加密扩展.在 PHP7.2 之后,Mcrypt 已经被 ...

  5. jquery监听动态添加的input的change事件

    使用下面方法在监听普通的input的change事件正常 $('#pp').on('change', 'input.videos_poster_input', function () { consol ...

  6. webpack4 使用babel处理ES6语法的一些简单配置

    一,安装包 npm install --save-dev babel-loader @babel/corenpm install @babel/preset-env --save-devnpm ins ...

  7. Springboot2.0整合Redis(注解开发)

    一. pom.xm文件引入对应jar包 <dependency> <groupId>org.springframework.boot</groupId> <a ...

  8. 11.5.1 LVS-DR 实验

    lvs-server VIP:10.211.55.99 DIP:10.211.55.23 负载均衡器   rs01 RIP:10.211.55.24 后端服务器   rs02 RIP:10.211.5 ...

  9. Java读取属性配置文件-properties

    在项目开发中,我们难免将一些可变的参数放在程序以外,作为一个单独的文件,即配置文件,这样方便项目在不同的使用环境部署时.或者说需要不同时,可以通过简单配置这些程序以外的文件来修改程序里的变量. 常用的 ...

  10. 深入理解Java虚拟机之垃圾回收篇

    垃圾回收简介 ​ Java 会对内存进行自动分配与回收管理,使上层业务更加安全,方便地使用内存实现程序逻辑.在不同的 JVM 实现及不同的回收机制中,堆内存的划分方式是不一样的. ​ 简要地介绍下垃圾 ...