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.中间的数是上面数和上面数左边的数 ...
随机推荐
- 蓝桥杯软件学院webserver,android
- c++设计模式15 --组合模式
今天研究了一下设计模式15 组合模式 本人是菜鸟一枚,所以一开始完全不懂组合究竟是什么意思.先上图一张,树形结构图: 文档说,如果想做出这样的结构,通常考虑组合模式.那是为什么呢?现在让我们看一下组合 ...
- Linq 导出Excel
var d = db.User; Repeater1.DataSource = d.ToList(); Repeater1.DataBind(); string guid = Guid.NewGuid ...
- windows phone 使用相机并获取图片(3)
原文:windows phone 使用相机并获取图片(3) 使用相机需要引用如下命名空间 " Margin="12,10,12,0" ></Image> ...
- JQuery日记_5.13 Sizzle选择器(六)选择器的效率
当选择表达式不符合高速匹配(id,tag,class)和原生QSA不可用或返回错误时,将调用select(selector, context, results, seed)方法,此方法迭代DO ...
- 使用Visual Studio 2010 创建简单的Silverlight应用程序
使用Visual Studio 2010 创建简单的Silverlight应用程序 Silverlight是创建动态的引人的RIAs(Rich Internet Application)的新方法.这里 ...
- Just like normal variables,
Just like normal variables, pointers can be declared constant. There are two different ways that poi ...
- 重新想象 Windows 8 Store Apps (2) - 控件之按钮控件: Button, HyperlinkButton, RepeatButton, ToggleButton, RadioButton, CheckBox, ToggleSwitch
原文:重新想象 Windows 8 Store Apps (2) - 控件之按钮控件: Button, HyperlinkButton, RepeatButton, ToggleButton, Rad ...
- C# Windows Phone 8 WP8 开发,取得手机萤幕大小两种方法。
原文:C# Windows Phone 8 WP8 开发,取得手机萤幕大小两种方法. 一般我们在开发Windows Phone App时,需要取得萤幕的大小来自定义最佳化控制项的大小,但是开如何取得萤 ...
- silverlight 和winform的结合使用
silverlight 和winform的结合使用比较简单,将silverlight承载在页面上,页面运行在winform上的webbrowser中即可. 这样的情况下,我没找到页面中silverli ...