lc 118 Pascal's Triangle


118 Pascal's Triangle

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

DP Accepted

这个问题就是杨辉三角,先初始化边界,再用递推式ans[i][j] = ans[i-1][j] + ans[i-1][j-1]递推即可得到答案。

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

LN : leetcode 118 Pascal's Triangle的更多相关文章

  1. leetcode 118. Pascal's Triangle 、119. Pascal's Triangle II 、120. Triangle

    118. Pascal's Triangle 第一种解法:比较麻烦 https://leetcode.com/problems/pascals-triangle/discuss/166279/cpp- ...

  2. LeetCode 118 Pascal's Triangle

    Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows  ...

  3. Leetcode#118. Pascal's Triangle(杨辉三角)

    题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...

  4. LeetCode 118. Pascal's Triangle (杨辉三角)

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

  5. leetcode 118 Pascal's Triangle ----- java

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

  6. Java [Leetcode 118]Pascal's Triangle

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

  7. Java for LeetCode 118 Pascal's Triangle

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

  8. Leetcode 118 Pascal's Triangle 数论递推

    杨辉三角,即组合数 递推 class Solution { vector<vector<int>> v; public: Solution() { ; i < ; ++i ...

  9. [LeetCode] 119. Pascal's Triangle II 杨辉三角 II

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

随机推荐

  1. Qt学习--初学注意事项

    过程.心得: 1)Qt Creator与相关的安装包的安装 我在选择去学习Qt之后,第一件事就是Qt SDK下载安装与配置.最初,在网上发现Qt使用的IDE环境        在Windows上可以选 ...

  2. VUEX action解除页面耦合

    最近项目中需要用到vue+vuex来实现登出跳转功能,老大指派任务要用action解除页面耦合,刚从vue深渊晕晕乎乎爬出来的我是一脸懵逼啊...啥是解除耦合...网上vuex的资料太少了,vuex手 ...

  3. Lnixu Bash

    一.简单命令 1.创建文件(vi) vi hellowold.txt2.创建目录(mkdir) mkdir linux_bash3.删除文件(rm) rm helloworld.txt4.复制文件(c ...

  4. LIKIE INSTR

    SELECT  url FROM test_url WHERE   FROM_UNIXTIME(create_time,'%Y%m%d %H') < '20171218 00'  AND  no ...

  5. Module in powershell

    https://docs.microsoft.com/en-us/powershell/module/powershellget/install-module?view=powershell-6 ht ...

  6. 调整多个控件的dock的顺序

    https://stackoverflow.com/questions/2607508/how-to-control-docking-order-in-winforms Go to View -> ...

  7. create database 默认utf-8

    CREATE DATABASE IF NOT EXISTS dbname DEFAULT CHARSET utf8 COLLATE utf8_general_ci; 这是sql语句 CREATE TA ...

  8. 浅入分析和Linux内核相关的文件夹/proc和/sys .

    近来文章被踩的厉害,我只想把自己从马哥学的知识自己总结下发到博客里,但是没想到昨天一篇文章刚发一上午被踩了9次...如果您真的觉得我的文章很烂,请告诉我原因,不胜感激. 这星期,马哥讲了关于Linux ...

  9. codeforces 125 A-E 补题

    A Measuring Lengths in Baden 进制转换 水题 #include<bits/stdc++.h> using namespace std; int main() { ...

  10. STM32F4 DMA2D_M2M_PFC

    此例程为STM324x9I_EVAL:DCMI_CaptureMode,使用的stm32f4xx_hal_driver, At each camera line event, the line is ...