LeetCode-118-Pascal's Triangle(帕斯卡的三角形)
2017-08-19 09:53
489 查看
Q:
Given numRows, generate the first numRows of Pascal’s triangle.
For example, given numRows = 5,
Return
Analysis:
基础的算法问题,中间元素值等于“肩上”两元素之和。
Code:
Given numRows, generate the first numRows of Pascal’s triangle.
For example, given numRows = 5,
Return
[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ]
Analysis:
基础的算法问题,中间元素值等于“肩上”两元素之和。
Code:
class Solution { public List<List<Integer>> generate(int numRows) { List<List<Integer>> out = new ArrayList<List<Integer>>(); if (numRows == 0) { return out; } else { for (int i = 1; i <= numRows; i++) { ArrayList<Integer> in = new ArrayList<Integer>(); for (int j = 0; j < i; j++) { if (j == 0 || j == i - 1) { in.add(1); } else { in.add(out.get(i - 2).get(j - 1) + out.get(i - 2).get(j)); } } out.add(in); } } return out; } }
相关文章推荐
- LeetCode(118) Pascal's Triangle (Java)
- LeetCode 118 Pascal's Triangle
- LeetCode - 118/119 - Pascal's Triangle
- leetcode118 and 119 Pascal's Triangle
- [leetCode]118_Pascal's Triangle
- [LeetCode]118 Pascal's Triangle
- leetcode Pascal's Triangle 帕斯卡尔三角形输出
- LeetCode 118: Pascal's Triangle
- [Leetcode 118 and 119, Easy] Pascal's Triangle (I and II)
- leetCode#118. Pascal's Triangle
- [LeetCode] 118. Pascal's Triangle
- Leetcode刷题记—— 118. Pascal's Triangle(杨辉三角形)
- leetcode_question_118 Pascal's Triangle
- [Leetcode] 118. Pascal's Triangle 解题报告
- leetcode118~Pascal's Triangle
- LeetCode 118 Pascal's Triangle
- Leetcode:118. Pascal's Triangle(JAVA)
- LeetCode-118:Pascal's Triangle (生成指定行数的杨辉三角)
- leetcode 118[easy]---Pascal's Triangle
- 【一天一道LeetCode】#118. Pascal's Triangle