LeetCode 杨辉三角

https://leetcode-cn.com/problems/pascals-triangle/description/

我的解决方案:下一层的每一个数字是该层的左上和右上之和

class Solution {
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> outter=new ArrayList<List<Integer>>();
        if(numRows==0)
            return outter;
        ArrayList<Integer> inner = new ArrayList<Integer>();
        inner.add(1);
        outter.add(inner);
        int outterIndex=0;
        while(outter.size()!=numRows) {
            ArrayList<Integer> lastLevel = (ArrayList<Integer>) outter.get(outterIndex);
            ArrayList<Integer> tmp = new ArrayList<Integer>();
            for(int i=0;i<lastLevel.size()+1;i++) {
                int left=i>0?lastLevel.get(i-1):0;
                int right=i<lastLevel.size()?lastLevel.get(i):0;
                int res=left+right;
                tmp.add(res);
            }
            outter.add(tmp);
            outterIndex++;
        }
        return outter;
    }
}

你可能感兴趣的:(leetcode-cn)