原题网址:https://leetcode.com/problems/pascals-triangle-ii/
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?
方法:
public class Solution {
public List<Integer> getRow(int rowIndex) {
int[] rows = new int[rowIndex+1];
int[] buf = new int[rowIndex+1];
for(int i=0; i<=rowIndex; i++) {
for(int j=0; j<=i; j++) {
if (j==0 || j==i) buf[j] = 1;
else buf[j] = rows[j-1] + rows[j];
}
int[] temp = rows;
rows = buf;
buf = temp;
}
List<Integer> result = new ArrayList<>(rowIndex+1);
for(int i=0; i<rows.length; i++) {
result.add(rows[i]);
}
return result;
}
}