一、     称号

一行值。

二、     分析

这道题跟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. SWT中在treeview中显示图片

    package com.repositoryclient.treeview; import org.eclipse.jface.resource.ImageDescriptor; import org ...

  2. AJAX POST请求中參数以form data和request payload形式在servlet中的获取方式

    HTTP请求中,假设是get请求,那么表单參数以name=value&name1=value1的形式附到url的后面,假设是post请求,那么表单參数是在请求体中,也是以name=value& ...

  3. redhat linux 5上创建本地yum源

    1.挂载光驱 [root@rh5rac1 ~]#mkdir -p /mnt/cdrom [root@rh5rac1 ~]#mount /dev/cdrom /mnt/cdrom 2.将redhat光盘 ...

  4. android_重写button样式

    这样的button样式应该源自IOS.假设安卓上实现,则须要使用android上面的layer-list来实现. 事实上layer-list有点像framlayout,作用就是覆盖. 先说一下实现原理 ...

  5. 解决错误 fatal error C1010: unexpected end of file while looking for precompiled head

    在编译VS时候,出现fatal error C1010: unexpected end of file while looking for precompiled head. 问题详解:致命错误C10 ...

  6. android LinearLayout等view如何获取button效果

    我们可以给LinearLayout以及一切继承自View的控件,设置View.onClickListener监听,例如LInearLayout. 但是我们发现LinearLayout可以执行监听方法体 ...

  7. ftk学习记(消息框篇)

    [ 声明:版权全部,欢迎转载,请勿用于商业用途.  联系信箱:feixiaoxing @163.com] 上一篇说到了输入框.闲话不多说,首先看结果显示, 大家看看效果是不是和我们之前说的一样.今天, ...

  8. Spring MVC helloWorld中遇到的问题及解决办法

    1.java.io.FileNotFoundException: Could not open ServletContext resource不能加载ServletContext的用法是配置到web. ...

  9. Git代理服务器设置和访问Github

    因为现在工作的网络环境有着非常严格的限制,.可以说,在最近的访问通过代理Github它采取了一些曲折的.也积累了一些相关经验.我们认为有必要注意什么. 符合"不要再发明轮子"宗旨, ...

  10. Android数据库专家秘籍(七)经验LitePal查询艺术

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/40153833 经过了多篇文章的学习,我们已经把LitePal中的绝大部分内容都掌握 ...