LN : leetcode 118 Pascal's Triangle
lc 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的更多相关文章
- 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- ...
- LeetCode 118 Pascal's Triangle
Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- Leetcode#118. Pascal's Triangle(杨辉三角)
题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...
- LeetCode 118. Pascal's Triangle (杨辉三角)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- leetcode 118 Pascal's Triangle ----- java
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- Java [Leetcode 118]Pascal's Triangle
题目描述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...
- Java for LeetCode 118 Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- Leetcode 118 Pascal's Triangle 数论递推
杨辉三角,即组合数 递推 class Solution { vector<vector<int>> v; public: Solution() { ; i < ; ++i ...
- [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, ...
随机推荐
- Servlet的引入
一.分析 此模式有问题: 1.jsp需要呼叫javabean StudentService stuService = new StudentServiceImpl(); List<Student ...
- 远程调试 Asp.Net 项目
项目部署到产品环境后,难免会发生一些故障,有一些可以在本地测试环境中直接重现,而有一些则无法重现.对于可以在本地测试环境中重现的Bug,开发人员往往能够很迅速地进行问题排查.而对于无法重现的Bug,就 ...
- Android AR场景拍照技术实现(有关键源代码)
ARVR技术交流群:129340649 欢迎增加. AR场景往往给别人留下的印象深刻,假设模型做的炫丽一点,效果将会更好. 那么怎样保存这一美好的情景呢?这篇文章将教你怎样实现AR场景的拍摄以及永久保 ...
- 2016/2/24 1,css有几种引入方式 2,div除了可以声明id来控制,还可以声明什么控制? 3,如何让2个div,并排显示。4,清除浮动 clear:left / right / both
1,css有几种引入方式 使用HTML标签的STYLE属性 将STYLE属性直接加在单个的HTML元素标签上,控制HTML标签的表现样式.这种引入CSS的方式是分散灵活方便,但缺乏整体性和规划性,不利 ...
- HttpsURLConnection 安全传输(HTTPS--Secure Hypertext Transfer Protocol-安全超文本传输协议)
HttpsURLConnection 扩展 HttpURLConnection,支持各种特定于 https 功能.此类使用 HostnameVerifier 和 SSLSocketFactory.为这 ...
- android编译openssl静态库.a
github上有一个开源项目,已经为你编译openssl建好了工程. 地址:https://github.com/aluvalasuman/OpenSSL1.0.1cForAndroid 选择需要的版 ...
- SPOJ:Labyrinth(最大直线)
The northern part of the Pyramid contains a very large and complicated labyrinth. The labyrinth is d ...
- linq to EF分组查询 group by 的使用
第一种:查询表达式语法: IQueryable<EnrollmentDateGroup> data = from student in db.Students group student ...
- 希尔排序(Shellsort)
首先,Shell是发明这个算法的人名,不是这个算法的思想或者特点. 希尔排序,也称为增量递减排序.基本思路,是把原来的序列,等效视为一个矩阵的形式.矩阵的列数,也称为宽度或者增量,记为w. 假设数组A ...
- bzoj 4785: [Zjoi2017]树状数组【树套树】
参考:https://www.cnblogs.com/ljh2000-jump/p/6686960.html 由于操作反过来了,所以显然树状数组维护后缀和,所以本来想查询(1,r)-(1,l-1),现 ...