[119]Pascal's Triangle II
2013-04-17 14:53
211 查看
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?
class Solution {
public:
vector<int> getRow(int rowIndex) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<int> result(rowIndex + 1);
for(int i = 0; i <= rowIndex; i++)
{
for(int j = i; j >= 0; j--)
{
if(j == 0 || j == i)
result[j] = 1;
else
result[j] = result[j] + result[j-1];
}
}
return result;
}
};
of the Pascal's triangle.
For example, given k = 3,
Return
[1,3,3,1].
Note:
Could you optimize your algorithm to use only O(k)
extra space?
class Solution {
public:
vector<int> getRow(int rowIndex) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<int> result(rowIndex + 1);
for(int i = 0; i <= rowIndex; i++)
{
for(int j = i; j >= 0; j--)
{
if(j == 0 || j == i)
result[j] = 1;
else
result[j] = result[j] + result[j-1];
}
}
return result;
}
};
相关文章推荐
- [Leetcode]#119 Pascal's Triangle II
- [leetcode] 119. Pascal's Triangle II
- 119. Pascal's Triangle II
- FTPrep, 119 Pascal's Triangle II
- 【Leetcode】119. Pascal's Triangle II
- [Array]-119. Pascal's Triangle II(@python)解题报告
- 119. Pascal's Triangle II
- leetcode119. Pascal's Triangle II
- Leetcode 119. Pascal's Triangle II
- 119. Pascal's Triangle II
- Leetcode 119. Pascal's Triangle II 杨辉三角2 解题报告
- 【LeetCode】119 Pascal's Triangle II
- LeetCode 119. Pascal's Triangle II
- 119. Pascal's Triangle II
- [leetCode 118 & 119] Pascal's Triangle I && II (杨辉三角问题)
- LeetCode 119. Pascal's Triangle II
- 119. Pascal's Triangle II
- leetcode 119. Pascal's Triangle II 杨辉三角形2
- 119. Pascal's Triangle II
- LeetCode119. Pascal's Triangle II