题目:

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的更多相关文章

  1. 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 [ ...

  2. 【Linked List Cycle II】cpp

    题目: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. ...

  3. 【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- ...

  4. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

  5. 【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, ...

  6. 【LEETCODE】34、119题,Pascal's Triangle II

    package y2019.Algorithm.array; import java.util.ArrayList; import java.util.List; /** * @ProjectName ...

  7. 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, ...

  8. 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 ...

  9. Pascal's Triangle,Pascal's Triangle II

    一.Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, giv ...

随机推荐

  1. Altium_Designer-如何放置总线

    在绘制原理图时我们避免不了要用到总线,但是在我们使用Altium Designer绘制总线时总是出现一些问题,导致总线无法使用.下面我就来帮助大家绘制出一条的总线,希望能够帮助在这方面摸索的人们. 第 ...

  2. WSL的unable to resolve host问题

    运行apt-get的时候提示 sudo: unable to resolve host DESKTOP-PS8VD9E 在 /etc/hosts文件中 127.0.0.1 对应主机名字给加一行就好了

  3. ie6下按钮下边框消失不显示的问题

    最近网站做改版,又发现一个ie6奇葩的问题,就一个很普通带边框的按钮,但在ie6中下边框不显示,ie7没有测试不知道是不是也不显示,其他浏览器正常 代码和预览效果如下: <style> b ...

  4. 有一个form,包含两个text,和两个按钮,当用户按第一个按扭时把数据提交到url1,按第二个按钮提交到url2,怎么实现呀?

    <form name="form1" method="post" action=""> <input type=" ...

  5. 1993: C语言实验——最值

    1993: C语言实验——最值 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 1541  Solved: 727[Submit][Status][Web ...

  6. 软件架构中的SOA架构有哪些特点?

    面向服务的架构(SOA)是一个组件模型,它将应用程序的不同功能单元(称为服务)通过这些服务之间定义良好的接口和契约联系起来.构建在各种各样的系统中的服务可以以一种统一和通用的方式进行交互. SOA是一 ...

  7. Python -函数的参数定义

    一.函数的参数有四种,位置参数.默认参数.可变参数和关键字参数 def func(x, y=0, *arg, **args): '''x为位置参数 y有默认值 *arg为可变参数 **args为关键字 ...

  8. javaweb基础(37)_mysql数据库自动生成主键

    测试脚本如下: 1 create table test1 2 ( 3 id int primary key auto_increment, 4 name varchar(20) 5 ); 测试代码: ...

  9. B1954 The xor-longest Path

    爆炸入口 给定一颗带权值的,节点数为n的树,求树上路径最大异或和. solution: 先dfs将所有点到根的异或和算出来.然后放进tire树中贪心. #include<cstdio> # ...

  10. 成员变量和成员函数前加static的作用?

    成员变量和成员函数前加static的作用?答:它们被称为常成员变量和常成员函数,又称为类成员变量和类成员函数.分别用来反映类的状态.比如类成员变量可以用来统计类实例的数量,类成员函数负责这种统计的动作 ...