86_leetcode_Pascal's Triangle II
2014-06-22 11:30
344 查看
Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return
Note:
Could you optimize your algorithm to use only O(k) extra space
1:分别考虑第一个元素,最后一个元素以及中间元素
vector<int> getRow(int rowIndex)
{
vector<int> result;
if(rowIndex < 0)
{
return result;
}
if(rowIndex == 0 )
{
result.push_back(1);
return result;
}
vector<int> temp;
for(int i = 1; i <= rowIndex; i++)
{
for(int j = 0; j <= i; j++)
{
if(j == 0 || j == i)
{
result.push_back(1);
}
else
{
result.push_back(temp[j-1] + temp[j]);
}
}
temp = result;
result.clear();
}
result = temp;
return result;
}
For example, given k = 3,
Return
[1,3,3,1].
Note:
Could you optimize your algorithm to use only O(k) extra space
1:分别考虑第一个元素,最后一个元素以及中间元素
vector<int> getRow(int rowIndex)
{
vector<int> result;
if(rowIndex < 0)
{
return result;
}
if(rowIndex == 0 )
{
result.push_back(1);
return result;
}
vector<int> temp;
for(int i = 1; i <= rowIndex; i++)
{
for(int j = 0; j <= i; j++)
{
if(j == 0 || j == i)
{
result.push_back(1);
}
else
{
result.push_back(temp[j-1] + temp[j]);
}
}
temp = result;
result.clear();
}
result = temp;
return result;
}
相关文章推荐
- [leetcode]: 119. Pascal's Triangle II
- (LeetCode)Pascal's Triangle II --- 杨辉三角进阶(滚动数组思想)
- [LeetCode]Pascal's Triangle II
- LeetCode -- Pascal's Triangle II
- [LeetCode]118. Pascal's Triangle&119. Pascal's Triangle II
- LeetCode-119. Pascal's Triangle II
- [LeetCode]-Pascal's Triangle I&II 杨辉三角问题
- Leetcode 119. Pascal's Triangle II
- leetcode-Pascal's Triangle II (2014.3.11)
- [C语言] leetcode 119. Pascal's Triangle II
- leetcode:Pascal's Triangle II 学会从后往前遍历
- [Leetcode] Pascal's Triangle II
- leetcode:pascal's_triangle_II
- [leetcode, python] Pascal's Triangle II 杨辉三角
- 【Leetcode】【python】Pascal's Triangle, Pascal's Triangle II
- LeetCode | Pascal's Triangle II
- LeetCode 119. Pascal's Triangle II
- LeetCode--No.119--Pascal's Triangle II
- 【一天一道LeetCode】#119. Pascal's Triangle II
- [LeetCode By Python]119. Pascal's Triangle II