LeetCode——Pascal's Triangle
Description:
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) { List<List<Integer>> list = new ArrayList<List<Integer>>(); for(int i=0; i<numRows; i++) { List<Integer> tList = new ArrayList<Integer>(); if(i == 0) { tList.add(1); } else { for(int j=0; j<=i; j++) { if(j == 0 || j == i) { tList.add(1); } else { tList.add(list.get(i-1).get(j) + list.get(i-1).get(j-1)); } } } list.add(tList); } return list; } }
作者:Pickle
声明:对于转载分享我是没有意见的,出于对博客园社区和作者的尊重一定要保留原文地址哈。
致读者:坚持写博客不容易,写高质量博客更难,我也在不断的学习和进步,希望和所有同路人一道用技术来改变生活。觉得有点用就点个赞哈。