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