首页 > 代码库 > [leetcode]Pascal's Triangle

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


import java.util.ArrayList;
import java.util.List;


public class Pascals_Triangle {

	public List<List<Integer>> generate(int numRows) {
        if(numRows <=0)
        	return new ArrayList();
        
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        for(int i=0; i<numRows; i++){
        	List<Integer> list = new ArrayList<Integer>();
        	if(result.size() == 0){
        		list.add(1);
        		result.add(list);
        	}
        	else{
        		list.add(1);
        		List<Integer> tmplist = result.get(result.size()-1);
        		for(int j = 0; j<tmplist.size()-1; j++){
        			int tmp = tmplist.get(j) + tmplist.get(j+1);
        			list.add(tmp);
        		}
        		list.add(1);
        		result.add(list);
        	}
        }
        return result;
    }
}





[leetcode]Pascal&#39;s Triangle