Given an index k, return the kth row of the Pascal's triangle
For example, when k = 3, the row is [1,3,3,1]- Ensure you have Java 8 installed ,
-
- Initialize the result
ArrayList<Integer> result = new ArrayList<>();
- Initialize the result
-
- Validate to ensure no kth value less than zero is given
if (rowIndex < 0) { return result; }
- Validate to ensure no kth value less than zero is given
-
- We then add the next element
result.add(1);
- We then add the next element
-
-
We then loop through against the number of rows to fnd the row listing the kth
for (int i = 1; i <= rowIndex; i++) { for (int j = result.size() - 2; j >= 0; j--) { result.set(j + 1, result.get(j) + result.get(j + 1)); } result.add(1); }
-
Owori Juma