【Leetcode】【Easy】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]
]
解题思路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的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【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 ...
- 【LeetCode每天一题】Pascal's Triangle(杨辉三角)
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's t ...
- 【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, ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 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 ...
- LeetCode Array Easy 118. Pascal's Triangle
Description Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. I ...
- 【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 ...
- 【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 ...
随机推荐
- vim(一) vim与markdown
vim markdown 配置 vim高亮显示Markdown语法 在.vimrc添加 Plugin 'godlygeek/tabular' Plugin 'plasticboy/vim-markdo ...
- day_01 python基础 基本数据类型 if条件
1. python简介 解释型,弱类型,高级开发语言2. 第一个python程序编写 1. 标点符号(英文) 2. 编码格式(utf-8)3.变量 把程序运行产生的中间值储存起来,方便后面 ...
- PIE SDK彩色空间变换
1. 算法功能简介 使用彩色空间变换工具可以将三波段红.绿.蓝图像变换到一个特定的彩色空间,并且能从所选彩色空间变换回 RGB.两次变换之间,通过对比度拉伸,可以生成一个色彩增强的彩色合成图像.此外, ...
- C#DataTable与Model互转
/// <summary> /// 实体转换辅助类 /// </summary> public class ModelConvertHelper<T> where ...
- 搭架Ubuntu的 apt-cacher服务
源服务器名称可能不太准确,意思是创建内网自己的私服,这样只要有Ubuntu通过该私服下载安装过软件,私服都会缓存,下一个Ubuntu的请求就直接从缓存中获取. 最近Ubuntu源服务器太慢了,北京的网 ...
- thinkphp执行流程
1. 入口文件index.php 用户对url的访问首先被定位到http://<serverIp>/<appName>/index.php, 这里的入口文件index.php做 ...
- php 不用第三个变量 交换两个变量的值汇总
//方法一:$a ="abc";$b="def"; $a = $a^$b;$b = $b^$a;$a = $a^$b; //方法二:list($a, $b)= ...
- 11、幻灯片:Slides
/* ---ts----*/ import { Page,Slides } from 'ionic-angular'; import { ViewChild } from '@angular/core ...
- Android4.4源码学习笔记
1.StatusBar和Recents是如何建立联系的 在BaseStatusBar的start()函数通过getComponent(RecentsComponent.class)得到了Recents ...
- 转载《学习HTML5 canvas遇到的问题》
学习HTML5 canvas遇到的问题 1. 非零环绕原则(nonzZero rule) 非零环绕原则是canvas在进行填充的时候是否要进行填充的判断依据. 在判断填充的区域拉一条线出来,拉到图形的 ...