首页 > 代码库 > Pascal's Triangle

Pascal's Triangle

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]
]

答案

public class Solution {
    public List<List<Integer>> generate(int numRows) {
        LinkedList<List<Integer>> result=new LinkedList<List<Integer>>();
        if(numRows<1)
        {
            return result;
        }
        List<Integer>p=new LinkedList<Integer>();
        p.add(1);
        result.add(p);
        for(int i=1;i<numRows;i++){
            p=result.get(i-1);
            List<Integer>current=new LinkedList<Integer>();
            current.add(1);
            for(int j=1;j<i;j++)
            {
                current.add(p.get(j-1)+p.get(j));
            }
            current.add(1);
            result.add(current);
        }
        return result;
    }
}


Pascal's Triangle