leetcode pascal's triangle

1, ArrayList 的用法 add,get

2,for 循环中,每执行一遍,注意i的值已经加1

public class Solution {
    public ArrayList<ArrayList<Integer>> generate(int numRows) {
        ArrayList<ArrayList<Integer>> result=new ArrayList<ArrayList<Integer>>();
        for(int i=1;i<=numRows;i++){
            ArrayList<Integer> hang=new ArrayList<Integer>();
            if(i==1){
                hang.add(1);
            }
            else{
                hang.add(1);
                for(int j=1;j<i-1;j++){
                    hang.add(result.get(i-2).get(j-1)+result.get(i-2).get(j));
                }
                hang.add(1);
            }
            result.add(hang);
        }
        return result;
    }
}
原文地址:https://www.cnblogs.com/lilyfindjobs/p/4058484.html