p119

LeetCode p119 Pascal’s Triangle II 题解

1.题目:

Given an index k, return the kth row 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?

题意:

输入一个数K 求杨辉三角形第K列【从0开始

2.解题思路:

见代码

3.代码


[title] [] [url] [link text]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
public class Solution {
public List<Integer> getRow(int rowIndex) {
List<Integer> ans = new ArrayList<Integer>();
ans.add(1);
for (int i = 1; i <= rowIndex; i++) {
for (int j = i - 1; j > 0; j--) {

ans.set(j, ans.get(j) + ans.get(j - 1));

}
ans.add(i, 1);
// System.out.println(Arrays.toString(ans.toArray()));
}
return ans;
}
}

4.一些总结: