0119 - Pascal's Triangle II
Given an integer rowIndex, return the rowIndexth (0-indexed) row of the Pascal's triangle.
In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:
Examples
Input: rowIndex = 3 Output: [1,3,3,1]
Input: rowIndex = 0 Output: [1]
Input: rowIndex = 1 Output: [1,1]
Constraints
0 <= rowIndex <= 33
Java Solution
class Solution {
public List<Integer> getRow(int rowIndex) {
List<Integer> curr = new ArrayList<>();
List<Integer> prev = new ArrayList<>();
curr.add(1);
prev.add(1);
for (int i = 1; i <= rowIndex; i++) {
curr = new ArrayList<>();
curr.add(1);
for (int j = 1; j < i; j++) {
curr.add(prev.get(j - 1) + prev.get(j));
}
curr.add(1);
prev = curr;
}
return prev;
}
}
Last updated