标签:leetcode
https://oj.leetcode.com/problems/pascals-triangle/
http://blog.csdn.net/linhuanmars/article/details/23311527
public class Solution { public List<List<Integer>> generate(int numRows) { List<List<Integer>> toReturn = new ArrayList<>(); if (numRows == 0) return toReturn; List<Integer> firstRow = Collections.<Integer> singletonList(1); toReturn.add(firstRow); for (int i = 1 ; i < numRows ; i ++) { List<Integer> lastRow = toReturn.get(toReturn.size() - 1); List<Integer> newRow = newRow(lastRow); toReturn.add(newRow); } return toReturn; } private List<Integer> newRow(List<Integer> lastRow) { int len = lastRow.size() + 1; List<Integer> toReturn = new ArrayList<>(len); for (int i = 0 ; i < len ; i ++) { toReturn.add(value(lastRow, i - 1) + value(lastRow , i)); } return toReturn; } private int value(List<Integer> v , int n) { if (n < 0 || n >= v.size()) return 0; return v.get(n); } }
[LeetCode]118 Pascal's Triangle
标签:leetcode
原文地址:http://7371901.blog.51cto.com/7361901/1599797