LeetCode 118 Pascal's Triangle
Problem:
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]
]
Summary:
输出杨辉三角的前n行。
Solution:
方法类似于LeetCode 119 Pascal's Triangle II
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> res;
vector<int> v;
for (int i = ; i < numRows; i++) {
v.resize(i + );
v[] = v[i] = ;
for (int j = i - ; j > ; j--) {
v[j] = v[j - ] + v[j];
}
res.push_back(v);
}
return res;
}
};
class Solution {public: vector<vector<int>> generate(int numRows) { vector<vector<int>> res; vector<int> v; for (int i = 0; i < numRows; i++) { v.resize(i + 1); v[0] = v[i] = 1; for (int j = i - 1; j > 0; j--) { v[j] = v[j - 1] + v[j]; } res.push_back(v); } return res; }};
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(杨辉三角)
题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...
- LN : leetcode 118 Pascal's Triangle
lc 118 Pascal's Triangle 118 Pascal's Triangle Given numRows, generate the first numRows of Pascal's ...
- 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, ...
随机推荐
- .Net配置中心-简介
系统简介 最近做了一个.Net配置中心,本质就是将原本放在各个站点下AppSettings中的配置统一管理,可以实现一次更改,自动更新,这里提供了两个版本, 一个是心跳版,一个是zookeeper版. ...
- CSS基本知识6-CSS字体
首先要考虑的是文本的排版,实际上有这玩意,Word还需要吗?不需要了.我们列下文本所涉及的操作: CSS 文本属性 属性 描述 color 设置文本颜色 direction 设置文本方向. line- ...
- 模仿iframe框架,由分隔栏动态改变左右两侧div大小———基于jQuery
<!DOCTYPE html><html lang="zh-cn"><head> <meta charset="utf-8&qu ...
- 【51Nod 1674】【算法马拉松 19A】区间的价值 V2
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1674 对区间分治,统计\([l,r]\)中经过mid的区间的答案. 我的 ...
- 计算机中位(bit), 字节(byte), 字(word)的关系
1.位(bit) 来自英文bit,音译为“比特”,表示二进制位.位是计算机内部数据储存的最小单位,11010100是一个8位二进制数.一个二进制位只可以表示0和1两种状态(21):两个二进制位可以表示 ...
- HDU 1817Necklace of Beads(置换+Polya计数)
Necklace of Beads Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- 理解DOM
http://www.cnblogs.com/chaogex/p/3959723.html 文档对象模型(Document Object Model,简称DOM),是W3C组织推荐的处理可扩展标志语言 ...
- win10 64位安装memcache扩展和开启redis扩展
前面有关于win10下搭建wamp环境的介绍,在此不在赘述,php操作memcache有memcache库和memcached库,其中memcache是php内置的扩展库,支持面向对象和面向过程两种操 ...
- EmguCV(OpenCV)实现高效显示视频(YUV)叠加包括汉字
视频处理中,往往需要在上面增加文字包括汉字英文字母数字标点等,Emgu.CV/opencv 绘图 线面文字包括中文 这篇里也有相关介绍,但是这篇里根据逐像素修改rgb值的方法效率太低 查了很多资料,基 ...
- Linux下php安装Redis扩展
说明: 操作系统:CentOS php安装目录:/usr/local/php php.ini配置文件路径:/usr/local/php7/etc/php.ini Nginx安装目录:/usr/loca ...