package T119杨辉三角II;
import java.util.LinkedList;
import java.util.List;
public class Solution {
public List<Integer> getRow(int rowIndex) {
List<Integer> oldList = new LinkedList<>();
oldList.add(1);
List<Integer> l = new LinkedList<>();
if (rowIndex == 0) {
return oldList;
}
for (int i = 1; i < rowIndex+2; i++) {
l.clear();
for (int j = 0; j < i; j++) {
l.add(1);
}
for (int j = 1; j < i - 1; j++) {
l.set(j, oldList.get(j - 1) + oldList.get(j));
}
oldList.clear();
for (int g : l) {
oldList.add(g);
}
}
return l;
}
public static void main(String args[]) {
Solution s = new Solution();
List<Integer> w = s.getRow(3);
w.forEach(integer -> System.out.println(integer));
}
}
```
class Solution:
def getRow(self, numRows: int):
if numRows == 0:
return list([1])
last_l = list()
l = list()
for i in range(1, numRows+2):
l = [1 for _ in range(i)]
# 表示第三层开始
if i > 2:
# 从该层的第1个数开始,len-2结束(len-1为最后一个元素)
for j in range(1, len(l) - 1):
l[j] = last_l[j - 1] + last_l[j]
last_l = l
return l