leetcode 生成杨辉三角形, 118 119 Pascal's Triangle 1,2
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]
]
解决方案:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> res = {};
for (int i = 0; i < numRows; i++) {
res.push_back(vector<int>(i + 1, 1));
for(int j = 1; j < i; j++) {
res[i][j] = (res[i - 1][j] + res[i - 1][j - 1]);
}
}
return res; }
Pascal's Triangle II
Total Accepted: 46342
Total Submissions: 157260
Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return [1,3,3,1]
.
Note:
Could you optimize your algorithm to use only O(k) extra space?
我的解决方案:
从没一行的倒数第二个算起,往前面逆推:
vector<int> getRow(int rowIndex)
{
vector<int> result(rowIndex + 1, 1); for(int i = 1; i <= rowIndex; ++i)
{
for(int j = i - 1; j > 0; --j)
{
result[j] = result[j] + result[j - 1];
}
} return result;
}
递归的解决方案:
vector<int> getRow(int rowIndex) {
vector<int> result; if (rowIndex == 0) {
result.push_back(1); return result;
} else {
vector<int> vec = getRow(rowIndex - 1);
result.push_back(1);
for (size_t i = 0; i < vec.size() - 1; i++) {
result.push_back(vec[i] + vec[i+1]);
}
result.push_back(1);
}
}
python 解决方案:
class Solution:
# @param {integer} rowIndex
# @return {integer[]}
def getRow(self, rowIndex):
row = [1]
for i in range(1, rowIndex+1):
row = list(map(lambda x,y: x+y, [0]+row, row + [0]))
return row
leetcode 生成杨辉三角形, 118 119 Pascal's Triangle 1,2的更多相关文章
- 【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 ...
- 118. 119. Pascal's Triangle -- 杨辉三角形
118. Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...
- 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, ...
- [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 ...
- LeetCode 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, ...
随机推荐
- 【SPOJ-GSHOP】Rama and Friends【贪心】【细节】
题意: 给出n个非严格递增的整数(可能有负数),必须操作k次.每次能够把当中一个数变为它的相反数,使得终于的数列和最大. 输出这个最大和. 考验怎样出坑数据卡自己的程序... #include < ...
- HDU 5296 Annoying problem dfs序 lca set
Annoying problem Problem Description Coco has a tree, whose nodes are conveniently labeled by 1,2,…, ...
- 云server之间实时文件同步和文件备份的最简单高效的免费方案
分布于不同云计算中心的多台云server,通常须要进行文件同步.以满足业务的须要. 传统的文件同步方案,部署繁琐.同步实时性差.无法令人惬意. 端端Clouduolc,一款纯p2p方式的文件实时 ...
- Android studio 分32位64位版本吗?
下载的时候,是不分32位和64位的.安装完成之后,在bin目录下,有studio.exe和studio64.exe这两个文件.前一个是32位的,后一个是64位的.根据自己的电脑进行选择.
- B树索引与索引优化
B树索引与索引优化 MySQL的MyISAM.InnoDB引擎默认均使用B+树索引(查询时都显示为“BTREE”),本文讨论两个问题: 为什么MySQL等主流数据库选择B+树的索引结构? 如何基于索引 ...
- Java中异常处理之try和catch代码块的使用
转自:https://www.jb51.net/article/72901.htm Java try和catch的使用 尽管由Java运行时系统提供的默认异常处理程序对于调试是很有用的,但通常你希望自 ...
- 最长回文子串 C++实现 java实现 leetcode系列(五)
给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1: 输入: "babad" 输出: "bab" 注意: &qu ...
- Ubuntu16.04下Mongodb官网安装部署步骤(图文详解)(博主推荐)
不多说,直接上干货! 在这篇博客里,我采用了非官网的安装步骤,来进行安装.走了弯路,同时,也是不建议.因为在大数据领域和实际生产里,还是要走正规的为好. Ubuntu16.04下Mongodb(离线安 ...
- Golang环境配置Centos
1.下载go程序包( go1.7rc1.linux-amd64.tar.gz)(http://www.golangtc.com/static/go/1.7rc1/go1.7rc1.linux-am ...
- org.apache.ibatis.binding.BindingException: Parameter ‘brOrderNo’ not found. Available parameters ar
最近使用 mybatis 写项目的时候遇到报错:org.apache.ibatis.binding.BindingException: Parameter 'brOrderNo' not found. ...