letcode 118

来源:互联网 发布:工作督促软件 编辑:程序博客网 时间:2024/04/30 11:11
问题描述:

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.*;class Solution {    public List<List<Integer>> generate(int numRows) { List<List<Integer>> list=new ArrayList<List<Integer>>();        for(int i=0;i<=numRows-1;i++){long num=1;ArrayList<Integer> al=new ArrayList<Integer>();for(int j=0;j<=i;j++){al.add((int)num);num=num*(i-j)/(j+1);}list.add(al);}return list;    }}class Test118 {public static void main(String[] args) {Solution s=new Solution();System.out.println(s.generate(5));}}


0 0