[LeetCode] Pascal’s Triangle II 解题报告
来源:互联网 发布:智能电视点歌软件 编辑:程序博客网 时间:2024/06/01 19:38
Given an index k, return the kth row of the Pascal’s triangle.
For example, given k = 3,
Return
Return
[1,3,3,1]
.[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
Note:
Could you optimize your algorithm to use only O(k) extra space?
Could you optimize your algorithm to use only O(k) extra space?
» Solve this problem
[解题报告]
滚动数组实现。注意Line11,要从后往前加,否则会产生冗余计算。
[Code]
1: vector<int> getRow(int rowIndex) {
2: // Start typing your C/C++ solution below
3: // DO NOT write int main() function
4: vector<int> result;
5: result.resize(rowIndex+2);
6: for(int i =0; i< rowIndex+2; i++)
7: result[i] = 0;
8: result[1]=1;
9: for(int i =0; i< rowIndex; i++)
10: {
11: for(int j =rowIndex+1; j>0; j--)
12: {
13: result[j] = result[j-1] + result[j];
14: }
15: }
16: result.erase(result.begin());
17: return result;
18: }
0 0
- 【LeetCode】Pascal's Triangle & II 解题报告
- 【LeetCode】Pascal's Triangle II 解题报告
- 【LeetCode】Pascal's Triangle II 解题报告
- [LeetCode] Pascal’s Triangle II 解题报告
- [Leetcode] 119. Pascal's Triangle II 解题报告
- [LeetCode],Pascal's Triangle, 解题报告
- [leetcode] 118. Pascal's Triangle 解题报告
- 【LeetCode】Pascal's Triangle 解题报告
- leetcode 118. Pascal's Triangle 解题报告
- [Leetcode] 118. Pascal's Triangle 解题报告
- Leetcode 119. Pascal's Triangle II 杨辉三角2 解题报告
- [leetcdoe] 119. Pascal's Triangle II 解题报告
- [Array]-119. Pascal's Triangle II(@python)解题报告
- Leetcode 118. Pascal's Triangle 杨辉三角 解题报告
- leetcode之Pascal's Triangle I 和 II 解题思路
- LeetCode Pascal's Triangle II
- LeetCode: Pascal's Triangle II
- [LeetCode] Pascal's Triangle II
- [LeetCode] Minimum Window Substring 解题报告
- [LeetCode] Multiply Strings 解题报告
- [LeetCode] Next Permutation 解题报告
- [LeetCode] Palindrome Number 解题报告
- [LeetCode] Partition List 解题报告
- [LeetCode] Pascal’s Triangle II 解题报告
- Android 之 Fresco 显示圆形图片 之坑
- [LeetCode] Path Sum II 解题报告
- [LeetCode] Permutations 解题报告
- Java环境变量的配置
- [LeetCode] Permutations II 解题报告
- [LeetCode] Plus One 解题报告
- [LeetCode] Populating Next Right Pointers in Each Node 解题报告
- [LeetCode] Populating Next Right Pointers in Each Node II 解题报告