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, ...
随机推荐
- C++ std::tr1::shared_ptr使用说明
1. 介绍 shared_ptr 是通过指针保持某个对象的共享拥有权的智能指针. 若干个 shared_ptr 对象能够拥有同一个对象:最后一个指向该对象的 shared_ptr 被销毁或重置时.该对 ...
- leetcode笔记:Majority Element
一. 题目描写叙述 Given an array of size n, find the majority element. The majority element is the element t ...
- 如何离线分析Kafka海量业务消息?1分钟快速为您支招
场景介绍 说起Kafka,许多使用者对它是又爱又恨.Kafka是一种分布式的.基于发布/订阅的消息系统,其极致体验让人欲罢不能,但操心的运维.复杂的安全策略.可靠性易用性的缺失等,仍需要使用者付出诸多 ...
- Orange's_1_win7下搭建环境
工欲善其事,必先利其器. 由于公司电脑工作环境是win7,为了学习于渊的Orange,所以就在windows下配置环境: 1.nasm: nasm汇编 http://www.nasm.us/ ...
- 更改android studio AVD 位置
- YTU 1003: Redraiment的遭遇
1003: Redraiment的遭遇 时间限制: 1000 Sec 内存限制: 128 MB 提交: 198 解决: 71 题目描述 Redraiment的老家住在工业区,日耗电量非常大.是政府 ...
- javascript函数参数、返回值类型检查
实现带参数.返回值类型声明的js函数: 类型定义:window.Str = Type.Str = Type.define('STRING', Type.isStr);var Per = Type.de ...
- Linux网络协议栈(四)——链路层(1)
1.接收帧当网络适配器接收到数据帧时,就会触发一个中断,中断处理程序执行一些需要及时处理的任务,然后在下半部进行其它可以延迟的处理.中断处理程序主要进行以下一些操作:(1) 分配sk_buff数 ...
- 深入理解JMM(Java内存模型) --(一)
并发编程模型的分类 在并发编程中,我们需要处理两个关键问题:线程之间如何通信及线程之间如何同步(这里的线程是指并发执行的活动实体).通信是指线程之间以何种机制来交换信息.在命令式编程中,线程之间的通信 ...
- Java-Runoob-高级教程-实例-字符串:12. Java 实例 - 字符串优化
ylbtech-Java-Runoob-高级教程-实例-字符串:12. Java 实例 - 字符串优化 1.返回顶部 1. Java 实例 - 字符串优化 Java 实例 以下实例演示了通过 Str ...