【Pascal's Triangle II 】cpp
题目:
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?
代码:
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> ret(rowIndex+,);
for ( int i=; i<=rowIndex; ++i )
{
for (int j=i-; j>; --j)
{
ret[j] = ret[j] + ret[j-];
}
}
return ret;
}
};
tips:
采用滚动数组技巧,可以缩减空间复杂度。
==========================================
第二次过这道题,题意一开始没有看清,改了一次AC了。
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> ret(rowIndex<?:rowIndex+,);
ret[] = ;
for ( int i=; i<=rowIndex; ++i )
{
for ( int j=i; j>; --j )
{
ret[j] = ret[j-] + ret[j];
}
}
return ret;
}
};
【Pascal's Triangle II 】cpp的更多相关文章
- leetcode 【 Pascal's Triangle II 】python 实现
题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [ ...
- 【Linked List Cycle II】cpp
题目: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. ...
- 【Reverse Linked List II】cpp
题目: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1- ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- 【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, ...
- 【LEETCODE】34、119题,Pascal's Triangle II
package y2019.Algorithm.array; import java.util.ArrayList; import java.util.List; /** * @ProjectName ...
- 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, ...
- 28. Triangle && Pascal's Triangle && Pascal's Triangle II
Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...
- Pascal's Triangle,Pascal's Triangle II
一.Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, giv ...
随机推荐
- python 网址
https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431752945034e ...
- DOM(四):h5扩展方法
getElementByClassName()方法getElementByClassName()方法接收一个参数,即一个包含一或多个类名的字符串,返回带有指定类的所有元素的NodeList //取得所 ...
- 用到UdpClient的一点经验
Thread.Abort对UdpClient.Receive阻塞的线程无效 http://computer-programming-forum.com/4-csharp/184f9d4ee63704f ...
- 【洛谷P1880】[NOI1995]石子合并
石子合并 fmax[l][r]表示合并区间[l,r]的最大分值, fmin[l][r]表示合并区间[l,r]的最小分值 for(k l~r-1) fmax[l][r]=max(fmax[l][r],f ...
- git常用命令(三)
====================================================================== 本地仓库操作 ====================== ...
- Nginx配置文件详细解释
转自:https://www.cnblogs.com/knowledgesea/p/5175711.html 序言 Nginx是lgor Sysoev为俄罗斯访问量第二的rambler.ru站点设计开 ...
- Vue 前端md5加密
用户注册时将加密后的密码发送给后端存储 当登陆的时候,再将加密后的密码和数据库中加密的密码相匹配. npm: https://www.npmjs.com/package/crypto-browseri ...
- 使用Jmeter性能测试,读取csv文件时的乱码问题
读取csv参数乱码问题 发送请求时参数通过CSV文件读取,发送请求后显示错误,把获取的参数通过在线urlencode转码器转码后发现是乱码.打开csv设值,编码格式选择的是UTF-8,打开参数文件后发 ...
- Mysql_Binary_Install_Scripts(采用二进制方式安装)
1.1 MYSQL实现代码 #!/bin/bash ######################################## #auth:wolf_dreams #time:2018-1 ...
- Fiddler(二)
该博客基于以下博客网站里的内容进行提取,实验,和补充.让我们开始 https://www.cnblogs.com/yyhh/p/5140852.html AutoResponder 允许拦截指定规则的 ...