【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 ...
随机推荐
- oracle数据库修改密码
忘记了数据库的登录密码,oracle数据库无法登录了. 先删除原先的密码保存文件: del E:\oracle_app\Administrator\product\11.2.0\dbhome_1\da ...
- 建立链表的虚拟头结点 203 Remove Linked List Element,82,147,148,237
该逻辑对于删除第一个元素不适用. 这样的代码不优美 /** * Definition for singly-linked list. * struct ListNode { * int val; * ...
- Linux kvm虚拟机的基本操作命令
Linux 虚拟化kvm virsh常用命令篇 1.创建虚拟机 virsh define damo.xml //创建的虚拟机不是活动的 virsh create damo.xml //创建的虚拟机是活 ...
- PIE SDK栅格分级渲染
1. 功能简介 栅格数据分级渲染是根据不同的分级规则,对像元值进行等级划分:并通过对每一级设置不同的显示符号和标注信息,从而达到分级显示的效果. 2.功能实现说明 2.1. 实现思路及原理说明 第一 ...
- AppInventor2笔记
将视觉化的 块语言 翻译为 Android上的实现 的编译器使用了Kawa语言框架,而Kawa是Scheme编程语言的方言,由Per Bothner开发,并由自由软件基金会发布,它是GNU操作系统的一 ...
- jdk监控tomcat
一, tomcat配置文件 在tomcat的配置文件中添加被监控的项 #在tomcat配置文件中开启监控功能 vim /application/tomcat/bin/catalina.sh +97 C ...
- 图解 I帧,B帧以及P帧
I‑frame (Intra-coded picture): 即完整的一张图片 P‑frame (Predicted picture): 与前面一张图片的区别的区域 B‑frame (Bidirect ...
- Java for循环的几种用法分析
J2SE 1.5提供了另一种形式的for循环.借助这种形式的for循环,可以用更简单地方式来遍历数组和Collection等类型的对象.本文介绍使用这种循环的具体方式,说明如何自行定义能被这样遍历的类 ...
- 新手 php连接数据库大概。简单过程浅析以及遇到的问题分析
原文作者:aircraft 原文地址: https://www.cnblogs.com/DOMLX/p/8116845.html 重点:PHP运行在服务器上的请记住!!! 1.在连接数据库与PHP之前 ...
- 1.Vue.js的常用指令
Vue.js介绍 Vue.js是当下很火的一个JavaScript MVVM库,它是以数据驱动和组件化的思想构建的.相比于Angular.js,Vue.js提供了更加简洁.更易于理解的API,使得 ...