题目链接:https://leetcode.com/problems/pascals-triangle-ii/
题目:
th
For example, given k = 3,
Return [1,3,3,1]
.
Note:
Could you optimize your algorithm to use only O(k) extra space?
思路:
要求空间复杂度为O(k),其实只需要记录前一行的结果就可以了。
算法:
[java] view plain copy
1. public List<Integer> getRow(int rowIndex) {
2. List<Integer> pre;
3. null;
4. for (int i = 1; i <= rowIndex + 1; i++) {
5. pre = cur;
6. new ArrayList<Integer>();
7. if (i == 1) {
8. 1);
9. else if (i == 2) {
10. 1);
11. 1);
12. else if (i > 2) {
13. 1);
14. for (int j = 0; j < pre.size() - 1; j++) {
15. 1));
16. }
17. 1);
18. }
19. }
20. return cur;
21. }