其他分享
首页 > 其他分享> > [简单] 118. 杨辉三角

[简单] 118. 杨辉三角

作者:互联网

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

 

对,没有灵魂的代码:

class Solution {
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> a = new ArrayList<>();

        List<Integer> prevRow = new ArrayList<>();
        for (int i = 1; i <= numRows; i++) {
            List<Integer> curRow = new ArrayList<>();
            if (i == 1) {
                curRow.add(1);
            }
            else {
                for(int n = 0; n < i; n++) {
                    if(n == 0 || n == i -1) {
                        curRow.add(1);
                    }
                    else {
                        Integer val = prevRow.get(n) + prevRow.get(n - 1);
                        curRow.add(val);
                    }
                }
            }

            a.add(curRow);
            prevRow.clear();// 清除上次记录.
            for (Integer e : curRow) {
                prevRow.add(e);
            }
            //curRow.clear();
        }

        return a;
    }
}
View Code

 

标签:int,List,add,prevRow,简单,杨辉三角,new,curRow,118
来源: https://www.cnblogs.com/chenxiaomai/p/16098248.html