题目
Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle.
Note that the row index starts from 0.
答案
class Solution {
public List<Integer> getRow(int rowIndex) {
List<Integer> row = new ArrayList<>();
row.add(1);
if(rowIndex == 0) return row;
for(int i = 1; i <= rowIndex; i++) {
int left = 0;
int row_size = row.size();
for(int j = 0; j <= row_size; j++) {
int curr = (j < row.size()) ? row.get(j) : 0;
if(j < row.size())
row.set(j, left + curr);
else
row.add(left + curr);
left = curr;
}
}
return row;
}
}
网友评论