问题描述:
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; } }
原文地址:http://blog.csdn.net/chenlei0630/article/details/40457101