[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; } }
posted on 2017-05-23 18:44 gavanwanggw 阅读(109) 评论(0) 编辑 收藏 举报