leetcode:pascal's_triangle_II
一、 称号
一行值。
二、 分析
这道题跟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's_triangle_II的更多相关文章
- LeetCode——Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- LeetCode——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 - Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- [LeetCode]Pascal's Triangle II
题目:给定一个行索引index,返回帕斯卡三角形第index层的三角形 算法:生成index层帕斯卡三角形,并返回第index层三角形 public class Solution { public L ...
- LeetCode:Pascal's Triangle I II
LeetCode:Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For examp ...
- 【Leetcode】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】118. Pascal's Triangle
@requires_authorization @author johnsondu @create_time 2015.7.23 19:54 @url [Pascal's Triangle](http ...
- leetcode笔记:Pascal's Triangle
一. 题目描写叙述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- leetcode:Pascal's Triangle
一. 题目 经典题目,杨辉三角,输入行数.生成杨辉三角的数组. 二. 分析 首先,我们知道有例如以下规律: 1.每一行的第一个数和最后一个数都为1 2.中间的数是上面数和上面数左边的数 ...
随机推荐
- Android网络服务发现(NSD)协议的使用
Android的网络服务发现协议(NSD)能够用于在小范围的网络中发现邻近设备上的某个应用.这对于一些社交网络.多人游戏类的应用会很有帮助. Android的NSD的用法大致上分为四种操作: 1. 注 ...
- Android Ant打包笔记
本文文档的下载地址(Word版):http://download.csdn.net/detail/yangwei19680827/7250711 Android Ant 打包 网上找了ant打包的资料 ...
- linux下安装cmake和mysql遇到的问题总结
首先是在安装cmake的过程中遇到的问题: 1.開始使用yum命令安装时,不知道为什么一直不行,然后就准备wget 来先下载压缩包,再手动编译. 因为网络限制,wget不能下载外网的东西一直显示con ...
- PowerDesigner 对 Oracle 作 逆向工程
原文 PowerDesigner 对 Oracle 作 逆向工程 目的 PowerDesigner 15对OracleClient 11g进行逆向工程 环境 Win7 64位系统 Oracle 11g ...
- Amazon AWS创建RHEL 7实例
在AWS上登录 如果没有账号的话先注册,参考 http://blog.banban.me/blog/2014/06/09/li-yong-awsmian-fei-zhang-hu-da-jian-vp ...
- 谷歌Web中国开发手册:1目的&夹
原版的:https://developers.google.com/web/fundamentals/getting-started/your-first-multi-screen-site/ 该网站 ...
- 直接插入排序、折半插入排序、Shell排序、冒泡排序,选择排序
一.直接插入排序 稳定,时间复杂度:最好O(n).最差O(n^2).平均O(n^2).空间复杂度O(1) void InsertSort(int L[], int n) { int i, j,key; ...
- Windows Phone开发(2):竖立自信,初试锋茫
原文:Windows Phone开发(2):竖立自信,初试锋茫 上一篇文章中,我们聊了一些"大炮"话题,从这篇文章开始,我们一起来学习WP开发吧. 一.我们有哪些装备. 安装完VS ...
- Cocos2d-x 脚本语言Lua中的面向对象
Cocos2d-x 脚本语言Lua中的面向对象 面向对象不是针对某一门语言,而是一种思想.在面向过程的语言也能够使用面向对象的思想来进行编程. 在Lua中,并没有面向对象的概念存在,没有类的定义和子类 ...
- VB6.0“挑衅”.NET!
来到与两年前接触VB,现在学习VB.NET,这两个看起来真的不得不说,这是相对的似(ps:一分之差,只有三个字母),计等.但他们有又什么不同呢?都说VB.NET高级,比VB究竟高级在哪里了?是不是VB ...