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

解题思路1:迭代插入(未通过)

[              [               [                      [
[1], [1], [1], [1],
[1], [1,1], [1,1], [1,1]
[1], ==> [1,2], ==> [1,2,1], ==>...==> [1,2,1],
[1], [1,3], [1,3,3], [1,3,3,1],
[1] [1,4] [1,4,6], [1,4,6,4,1],
] [ [ [

可以推导出每一列的递推公式:

第一列从第一行开始:1

第二列从第二行开始:j / 1,  j∈[1,n)

第三列从第三行开始:[j*(j-1)] / [1*2], j∈[2,n)

...

第M列从第M行开始:[j*(j-1)*...*(j-m+2)] / [1*2*...*m-1], j∈[m,n)

但是程序未能实现,在输入numRows=8时,出现Runtime Error错误。

【★】解题思路2:

用f(n)(m)表示第n行第m个元素,n从1开始,1≤m≤n;

则f()(1)=f()(n)=1, f(n)(m)=f(n-1)(m-1) + f(n-1)(m);

核心代码只有四步:

①新建符合的空间;

②将首尾置为1;

③遍历除首尾外的元素空间,利用公式填充;

④将填充好的列表放入结果队列里;

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

附录:

杨辉三角与计算机

【Leetcode】【Easy】Pascal's Triangle的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. 【LeetCode每天一题】Pascal's Triangle(杨辉三角)

    Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's t ...

  5. 【leetcode刷题笔记】Pascal's Triangle II

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

  6. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  7. LeetCode Array Easy 119. Pascal's Triangle II

    Description Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's tria ...

  8. LeetCode Array Easy 118. Pascal's Triangle

    Description Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. I ...

  9. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

  10. 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman

    [Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...

随机推荐

  1. Wamp环境域名重定向问题

    打开浏览器输入地址跳转不到页面,还得再次输入www后的内容, 解决办法: D:\wamp\bin\apache\apache2.4.9\conf(这里安装在D盘所以是D) 目录中文件:httpd.co ...

  2. 方格填数--蓝桥杯---dfs

    答案:1580 相似题目:N皇后问题 注意要枚举的是什么 #include<iostream> #include<string.h> using namespace std; ...

  3. jvm双亲委派模型

    其实,双亲委派模型并不复杂.自定义类加载器也不难!随便从网上搜一下就能搜出一大把结果,然后copy一下就能用.但是,如果每次想自定义类加载器就必须搜一遍别人的文章,然后复制,这样显然不行.可是自定义类 ...

  4. (转)SSH批量分发管理&非交互式expect

    目录 1 SSH批量分发管理 1.1 测试环境 1.2 批量管理步骤 1.3 批量分发管理实例 1.3.1 利用sudo提权来实现没有权限的用户拷贝 1.3.2 利用sudo提权开发管理脚本 1.3. ...

  5. setContentView和inflate区别

    一般用LayoutInflater做一件事:inflate inflate这个方法总共有四种形式(见下面),目的都是把xml表述的layout转化为View对象.其中有一个比较常用,View infl ...

  6. 【ExtJS】关于自定义组件(一)

    一.目的: ExtJS中提供了下拉日期选择控件Ext.form.field.Date与下拉时间选择控件Ext.form.field.Time.不过没有一个在选择日期时选择时间的控件datetimefi ...

  7. 在ZYNQ-7000平台上利用PS点亮PL上的LED灯

    在ZYNQ-7000平台上利用PS点亮PL上的LED灯 1.实验方案 图1    实验方案系统框图 2.具体步骤 2.1.vivado工程建立 ①打开vivado集成开发环境,点击“Create Pr ...

  8. java io 学习笔记(一)

    java的IO操作都在java.io包下面,这个包下面有12个接口和而是多各类,类从读写的角度可以分为两种,一种是用于读,一种是用于写:从字符流字节流的角度,也可以分为两种,一种和字符有关,一种和字节 ...

  9. 游戏AI的生命力源自哪里?为你揭开MOBA AI的秘密!

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由wataloo发表在专栏wataloo的试验田 1 设计概要 1.1 设计原则和目的 英雄AI的目的主要有: 1.新手过渡局,让玩家刚 ...

  10. nagios监控远程端口

    check_port 位置:/usr/local/nagios/libexec/ 代码(新建可执行文件) #!/bin/sh /usr/local/nagios/libexec/check_tcp - ...