Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
Return
[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ]
Java:
1. 思路清晰 简洁 好理解 真的是征服代码:
http://blog.youkuaiyun.com/linhuanmars/article/details/23311527
public class Solution {
public ArrayList<ArrayList<Integer>> generate(int numRows) {//List是抽象类,所以还是得换成arraylist
ArrayList<ArrayList<Integer>> res= new ArrayList<ArrayList<Integer>>();
if(numRows<=0)
return res;
ArrayList<Integer> pre= new ArrayList<Integer>();
pre.add(1);
res.add(pre);
for(int i=2;i<=numRows;i++)
{
ArrayList<Integer> cur= new ArrayList<Integer>();
cur.add(1);
for(int j=0;j<pre.size()-1;j++)
{
cur.add(pre.get(j)+pre.get(j+1));
}
cur.add(1);
res.add(cur);
pre=cur;
}
return res;
}
}