ID | Title | Difficulty | |
---|---|---|---|
Loading... |
119. Pascal's Triangle II
Easy
LeetCode
Array, Dynamic Programming
Problem
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.
In Pascal’s triangle, each number is the sum of the two numbers directly above it.
Example:
Input: 3
Output: [1,3,3,1]
Code
class Solution {
public List<Integer> getRow(int rowIndex) {
List<Integer> list = new ArrayList<>();
for(int i = 0; i <= rowIndex; i++){
// 在开头插入1
list.add(0, 1);
// 1,2,1 -> 1,1,2,1
// 1,3,3,1
for(int j = 1; j < list.size() - 1; j++){
list.set(j, list.get(j) + list.get(j + 1));
}
}
return list;
}
}
按 <- 键看上一题!
118. Pascal's Triangle
按 -> 键看下一题!
120. Triangle