【Leetcode】Pascal's Triangle II (Pascal)
2014-10-27 01:48
267 查看
这道题和I差不多,某种程度上它比I还要简单些,因为它是一维的。
每次只需要从最后一位开始往前更新就可以了,然后再最后一位加上1。
比如【1】更新至【1,1】更新至【1,2,1】更新至【1,3,3,1】。。。
更新的方法是让指针j指向末尾,然后用result[j] = result[j] + result[j-1]就可以了。
每次只需要从最后一位开始往前更新就可以了,然后再最后一位加上1。
比如【1】更新至【1,1】更新至【1,2,1】更新至【1,3,3,1】。。。
更新的方法是让指针j指向末尾,然后用result[j] = result[j] + result[j-1]就可以了。
public ArrayList<Integer> getRow(int rowIndex) { ArrayList<Integer> result = new ArrayList<Integer>(); for (int i = 0; i <= rowIndex; i++) { for (int j = result.size() - 1; j > 0; j--) result.set(j, result.get(j) + result.get(j - 1)); result.add(1); } return result; }
相关文章推荐
- LeetCode-119:Pascal's Triangle II (返回杨辉三角指定行)
- leetcode--Pascal's Triangle II
- [leetcode, python] Pascal's Triangle II 杨辉三角
- leetcode:Pascal's Triangle II
- Leetcode Pascal's Triangle II
- [Leetcode]Pascal's Triangle II
- LeetCode 33 Pascal's Triangle II
- Leetcode 119. Pascal's Triangle II
- 119. Pascal's triangle II Leetcode Python
- [Leetcode]119. Pascal's Triangle II
- leetcode: Pascal's Triangle II
- leetcode 119. Pascal's Triangle II
- Leetcode 119. Pascal's Triangle II
- LeetCode119—Pascal's Triangle II
- LeetCode 119. Pascal's Triangle II
- [Leetcode] 119. Pascal's Triangle II 解题报告
- Pascal's Triangle II(LeetCode)
- LeetCode 119. Pascal's Triangle II
- [leetcode, python] Pascal's Triangle II 杨辉三角
- 【LeetCode-119】 Pascal's Triangle II(C++)