LeetCode Pascal's Triangle II
2016-01-05 17:29
120 查看
原题链接在这里:https://leetcode.com/problems/pascals-triangle-ii/
与Pascal's Triangle相似。用上一行作为历史记录算下一行,因为需要使用前面的历史数据,所以要从后往前更新res.
Time Complexity: O(n^2). It doesn't need extra space.
AC Java:
与Pascal's Triangle相似。用上一行作为历史记录算下一行,因为需要使用前面的历史数据,所以要从后往前更新res.
Time Complexity: O(n^2). It doesn't need extra space.
AC Java:
public class Solution { public List<Integer> getRow(int rowIndex) { List<Integer> res = new ArrayList<Integer>(); if(rowIndex < 0){ return res; } res.add(1); for(int i = 0; i<rowIndex; i++){ //因为这里需要使用前面的历史数据,所以不能从左到右更新,必须从右向左更新 for(int j = res.size()-1; j>0; j--){ res.set(j, res.get(j)+res.get(j-1)); } res.add(1); } return res; } }
相关文章推荐
- H5与Native交互的实现
- kuangbin_ShortPath L (POJ 2502)
- 图解linux下top命令的使用
- Spring注入方式
- 计算二叉树的宽度的两种方式
- 关于CString的函数用法
- [jQuery] Selector
- 并行进行
- Gitlab安装的时候碰到的问题
- Json数据解析模拟美团界面显示
- Linux---YUM
- 二进制枚举
- 计算二叉树的宽度的两种方式
- UIApearance
- 横向UITableView的实现和FPPopover的介绍
- C# 原样复制excel工作表
- 时间日期Date类型
- JBoss的安装与配置(对应eclipse配置)【转】
- Java中的System类
- python字符串连接方式(转)