一、     称号

一行值。

二、     分析

这道题跟Pascal'sTriangle非常类似,仅仅是这里仅仅须要求出某一行的结果。Pascal's Triangle中由于是求出所有结果,所以我们须要上一行的数据就非常自然的能够去取。而这里我们仅仅须要一行数据,就得考虑一下是不是能仅仅用一行的空间来存储结果而不须要额外的来存储上一行呢?

这里确实是能够实现的。对于每一行我们知道假设从前往后扫,第i个元素的值等于上一行的ans[i]+ans[i+1],能够看到数据是往前看的,假设我们仅仅用一行空间,那么须要的数据就会被覆盖掉。所以这里採取的方法是从后往前扫,这样每次须要的数据就是ans[i]+ans[i-1],我们须要的数据不会被覆盖,由于须要的ans[i]仅仅在当前步用,下一步就不须要了。这个技巧在动态规划省空间时也常常使用,主要就是看我们须要的数据是原来的数据还是新的数据来决定我们遍历的方向。时间复杂度还是O(n^2)。而空间这里是O(k)来存储结果,仍然不须要额外空间。

比如:当行数为5时

0  :   0 00 0 0

1  :   1
00 0 0

2  :   1 10 0 1

3  :   1 21 0 1

4  :   1 33 1 1

5  :   1 46 4 1

可知红色部分为标准的杨辉三角,所以不难理解从后往前的遍历方式。

class Solution {
public:
vector<int> getRow(int rowIndex) {
if (rowIndex < 0) return vector<int>();
vector<int> res(rowIndex + 1);
if (rowIndex == 0)
{
res[0] = 1;
return res;
} int t1, t2; for (int i = 1; i <= rowIndex; i++)
{
res[0] = res[i] = 1;
t1 = res[0];
t2 = res[1];
for (int j = 1; j < i; j++)
{
res[j] = t1 + t2;
t1 = t2;
t2 = res[j + 1];
}
}
return res;
}
}; class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> ans(rowIndex+1,1);
for(int i=0;i<=rowIndex;i++) {
for(int j=i-1;j>=1;j--) {
ans[j]=ans[j]+ans[j-1];
}
}
return ans;
}
}; class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> ans;
if(rowIndex <0) return ans;
ans.push_back(1);
for(int i=1;i<=rowIndex;i++) {
for(int j=ans.size()-2;j>=0;j--) {
ans[j+1]=ans[j]+ans[j+1];
}
ans.push_back(1);
}
return ans;
}
};

版权声明:本文博主原创文章,博客,未经同意不得转载。

leetcode:pascal&#39;s_triangle_II的更多相关文章

  1. LeetCode——Pascal&#39;s Triangle

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...

  2. LeetCode——Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  3. leetcode - Pascal&#39;s Triangle

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...

  4. [LeetCode]Pascal&#39;s Triangle II

    题目:给定一个行索引index,返回帕斯卡三角形第index层的三角形 算法:生成index层帕斯卡三角形,并返回第index层三角形 public class Solution { public L ...

  5. LeetCode:Pascal's Triangle I II

    LeetCode:Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For examp ...

  6. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  7. 【leetcode】118. Pascal&#39;s Triangle

    @requires_authorization @author johnsondu @create_time 2015.7.23 19:54 @url [Pascal's Triangle](http ...

  8. leetcode笔记:Pascal&#39;s Triangle

    一. 题目描写叙述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...

  9. leetcode:Pascal&#39;s Triangle

    一.     题目 经典题目,杨辉三角,输入行数.生成杨辉三角的数组. 二.     分析 首先,我们知道有例如以下规律: 1.每一行的第一个数和最后一个数都为1 2.中间的数是上面数和上面数左边的数 ...

随机推荐

  1. 【HDU】4888 Redraw Beautiful Drawings 网络流【推断解是否唯一】

    传送门:pid=4888">[HDU]4888 Redraw Beautiful Drawings 题目分析: 比赛的时候看出是个网络流,可是没有敲出来.各种反面样例推倒自己(究其原因 ...

  2. 重温delphi之控制台程序:Hello World!

    原文:重温delphi之控制台程序:Hello World! 这二天用c#开发ActiveX时,发现不管怎么弄,c#就是没办法生成ocx的纯正activeX控件,而且还要强迫用户安装巨大的.net f ...

  3. linux crontab定时执行shell脚本

    linux下使用crontab命令被用来提交和管理用户的需要周期性执行的任务,示例如下:crontab -e 编辑周期任务30 21 * * * /etc/init.d/smb restart 每晚的 ...

  4. 成为JAVA软件开发工程师要学哪些东西

    2010-04-22 15:34 提问者采纳 Java EE(旧称j2ee)   第一阶段:Java基础,包括java语法,面向对象特征,常见API,集合框架: *第二阶段:java界面编程,包括AW ...

  5. MYSQL中的字符串连接符

    update `table` set nsdf = concat('a','b') where id=137

  6. JS类定义方式

    // 方法1 对象直接量 var obj1 = { v1 : "", get_v1 : function() { return this.v1; }, set_v1 : funct ...

  7. XP下採用DirectShow採集摄像头

    转载请标明是引用于 http://blog.csdn.net/chenyujing1234 欢迎大家提出意见,一起讨论! 须要演示样例源代码的请独自联系我. 前提: 摄像头能正常工作.摄像头有创建di ...

  8. hdu3873 有约束条件的最短路

    题目大意:美国佬打算入侵火星,火星上有n个城市,有些城市可能受其他城市保护, 如果i城市受j城市保护,那么你必须先攻占j城市才能再攻占i城市,问你攻占城市n的最短时间是多少. 数据解释: 给定t, 表 ...

  9. CentOS6.5解压缩文件.tar.gz .war .zip

    拉开拉链.tar.gz文件: tar -zxvf web.tar.gz tar将文件解压缩到一个指定的文件夹. 拉开拉链.war .zip文件到指定的文件夹: unzip web.war -d web ...

  10. atitit.报告最佳实践oae 和报告引擎的选择

    atitit.报告最佳实践oae 与报表引擎选型 1. 报表的基本的功能and结构 2 1.1. 查询设计器(配置化,metadata in html) ,anno 2 1.2. 查询引擎 2 1.3 ...