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, ...
随机推荐
- 一个IM开源项目LiteTalk
http://blog.csdn.net/visualwind/article/details/6086631 http://blog.sina.com.cn/s/blog_54b5ea250101n ...
- udhcp源码详解(四) 之租赁IP的管理
Server端对于租赁出去的IP的管理是基于结构体dhcpOfferedAddr的,该结构体的定义是在leases.c文件里:(结构体的成员介绍说明见详解之数据结构) 1: struct dhcpOf ...
- 在webkit中如何避免触发layout(重排)
很多web开发者都已经意识到,在脚本执行中,DOM操作的用时可能比js本身执行时间要长很多,其中潜在的消耗基本上是由于触发了layout(即重排reflow:由DOM树构建为Render渲染树的过程) ...
- SSH三大框架整合配置详细步骤(2)
4 配置Hibernate Hibernate MySql连接配置 在Hibernate中,可以配置很多种数据库,例如MySql.Sql Server和Oracle,Hibernate MySql连接 ...
- C 编程中fseek、ftell的用法总结
fseek 函数功能是将文件指针移动到指定的地方,因此可以通过fseek重置文件指针的位置.函数原型: int fseek(FILE *stream, long offset, int origi ...
- linux文件读写 文件锁、select、poll【转】
本文转载自:http://blog.csdn.net/fansongy/article/details/6853395 一.文件锁 文件锁用于多个用户共同使用或操作同一个文件.有读锁的时候可以再加读锁 ...
- mysql sakila 执行失败
1.下载 https://dev.mysql.com/doc/index-other.html 2.解压 3.将解压的文件放入某个位置,必须tmp下面 4.登录mysql 进行source处理 mys ...
- try-with-resources使用示例
try (InputStream is = new FileInputStream("test")) { is.read(); ... } catch(Exception e) { ...
- Linux 下的静态(函数)库、动态(函数)库
0. 基本 在命名上,静态库的名字一般是 libxxx.a,动态库的名字一般是 libxxx.so,有时 libxxx.so.major.minor,xxx 是该 lib 的名字,major 是主版本 ...
- 【SCOI 2005】 扫雷
[题目链接] 点击打开链接 [算法] 只要第一行第一个数确定了,后面的数也都确定了 递推两遍即可 [代码] #include<bits/stdc++.h> using namespace ...