首页 > 代码库 > [LeetCode]118 Pascal's Triangle
[LeetCode]118 Pascal's Triangle
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。