今天的題目為119.Pascal's Triangle II,這一題是上一題的延伸,目的在回傳指定的一行(rowIndex)。
以下為程式碼:
class Solution {
public List<Integer> getRow(int rowIndex) {
List<Integer> row =
new ArrayList<>(Collections.nCopies(rowIndex + 1, 0));
row.set(0, 1);
for (int i = 1; i <= rowIndex; i++) {
for (int j = i; j >= 1; j--) {
row.set(j, row.get(j) + row.get(j - 1));
}
}
return row;
}
}