LeetCode 118. Pascal's Triangle

来源:互联网 发布:mac系统的网游 编辑:程序博客网 时间:2024/05/16 23:01
public class Solution {    public List<List<Integer>> generate(int numRows) {        List<List<Integer>> list = new ArrayList<List<Integer>>();        int[] lastRow = null;        for (int i = 0; i < numRows; i++) {        int[] newRow = new int[i + 1];        if (lastRow == null) newRow[0] = 1;        else {        for (int j = 0; j < i + 1; j++) {        if (j == 0 || j == i) newRow[j] = 1;        else newRow[j] = lastRow[j - 1] + lastRow[j];        }        }        lastRow = newRow;        List<Integer> newList = new ArrayList<Integer>();        for (int j : newRow) newList.add(j);        list.add(newList);        }        return list;    }}

0 0
原创粉丝点击