118. 119. Pascal's Triangle -- 杨辉三角形
118.
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]
]
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> ans;
if(numRows < )
return ans;
vector<int> pre, cur;
pre.push_back();
ans.push_back(pre);
for(int i = ; i < numRows; i++)
{
cur.push_back();
for(int j = ; j < pre.size()-; j++)
{
cur.push_back(pre[j]+pre[j+]);
}
cur.push_back();
ans.push_back(cur);
pre = cur;
cur.clear();
}
return ans;
}
};
119.
Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return [1,3,3,1]
.
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> ans(rowIndex+, );
ans[] = ;
for(int i = ; i <= rowIndex; i++)
{
ans[i] = ;
for(int j = i-; j > ; j--)
{
ans[j] += ans[j-];
}
}
return ans;
}
};
只开一个数组,从后向前计算。
118. 119. Pascal's Triangle -- 杨辉三角形的更多相关文章
- 【LeetCode】118 & 119 - Pascal's Triangle & Pascal's Triangle II
118 - Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, ...
- 118/119. Pascal's Triangle/II
原文题目: 118. Pascal's Triangle 119. Pascal's Triangle II 读题: 杨辉三角问题 '''118''' class Solution(object): ...
- leetcode 生成杨辉三角形, 118 119 Pascal's Triangle 1,2
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- leetcode 生成杨辉三角形, 118 119 Pascal's Triangle 1,2
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- 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 OJ 119. 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, ...
- 118. Pascal's Triangle杨辉三角形(全部/一行)
[抄题]: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...
- [LeetCode] 119. Pascal's Triangle II 杨辉三角之二
Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note t ...
- [LeetCode]题解(python):119 Pascal's Triangle II
题目来源 https://leetcode.com/problems/pascals-triangle-ii/ Given an index k, return the kth row of the ...
随机推荐
- Java EE 在网页输出九九乘法表
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- 【转】Kali Linux 新手折腾笔记
原作者:http://defcon.cn/1618.html 最近在折腾Kali Linux 顺便做一简单整理,至于安装就不再多扯了,估计会出现的问题上一篇文章<VMware虚拟机安装Kali ...
- QWebView下载文件,QUrl中解析文件名
参考网址: http://blog.csdn.net/cdnight/article/details/23658715 http://www.tuicool.com/articles/AzeaUz h ...
- C# 线程(一)
From : http://www.cnblogs.com/miniwiki/archive/2010/06/18/1760540.html 文章系参考转载,英文原文网址请参考:http://www. ...
- Linux中变量#,@,0,1,2,*,$$,$?的含义
$# 是传给脚本的参数个数 $ 是脚本本身的名字 $ 是传递给该shell脚本的第一个参数 $ 是传递给该shell脚本的第二个参数 $@ 是传给脚本的所有参数的列表 $* 是以一个单字符串显示所有向 ...
- iOS - OC NSNull 空值
前言 @interface NSNull : NSObject <NSCopying, NSSecureCoding> 作为占据空间的一个空值,如用在数组或字典中占据一个没有任何值的空间. ...
- PHP面向对象(OOP)编程入门教程————如何实例化对象?
我们上面说过面向对象程序的单位就是对象,但对象又是通过类的实例化出来的,既然我们类会声明了,下一步就是实例化对象了. 当定义好类后,我们使用new关键字来生成一个对象. $对象名称 = new 类名称 ...
- Python学习(7)数字
目录 Python 数字 Python 数字类型转换 Python 数学函数 Python 随机数函数 Python 三角函数 Python 数学常量 Python 数字 Python 数字数据类型用 ...
- android模拟器中文乱码
问题:在xml文件中设置的中文能正确输出,但是在java文件中设置的中文会在模拟器上呈现乱码 解决方案:在build.gradle文件中添加一行代码 android {compileOptions.e ...
- Java 文件IO续
文件IO续 File类 用来将文件和文件夹封装成对象 方便对文件和文件夹的属性信息进行操作 File对象可以作为参数传递给流的构造函数 Demo1 File的构造方法 public cla ...