编程开发Pascal's Triangle解析,GivennumRows, generate the firstnumRowsof Pascal's triangle.
For example, givennumRows= 5,
Return
[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ]杨辉三角。程序如下:
class Solution { public List<>> generate(int numRows) { List list = new ArrayList<>(); List<> > llist = new ArrayList<>(); if (numRows == 0){ return llist; } for (int i = 1; i <= numRows; ++ i){ for (int j = 0; j < i; ++ j){ if (j == 0){ list.add(1); } else if (j == i - 1){ list.add(1); } else { List tmp = llist.get(i-2); list.add(tmp.get(j - 1) + tmp.get(j)); } } llist.add(list); list = new ArrayList<>(); } return llist; } }