首页 > 其他 > 详细

LeetCode Pascal's Triangle II

时间:2016-01-05 18:19:07      阅读:124      评论:0      收藏:0      [点我收藏+]

原题链接在这里:https://leetcode.com/problems/pascals-triangle-ii/

Pascal‘s Triangle相似。用上一行作为历史记录算下一行,因为需要使用前面的历史数据,所以要从后往前更新res.

Time Complexity: O(n^2). It doesn‘t need extra space.

AC Java:

 1 public class Solution {
 2     public List<Integer> getRow(int rowIndex) {
 3         List<Integer> res = new ArrayList<Integer>();
 4         if(rowIndex < 0){
 5             return res;
 6         }
 7         res.add(1);
 8         for(int i = 0; i<rowIndex; i++){
 9             //因为这里需要使用前面的历史数据,所以不能从左到右更新,必须从右向左更新
10             for(int j = res.size()-1; j>0; j--){
11                 res.set(j, res.get(j)+res.get(j-1));
12             }
13             res.add(1);
14         }
15         
16         return res;
17     }
18 }

 

LeetCode Pascal's Triangle II

原文:http://www.cnblogs.com/Dylan-Java-NYC/p/5103170.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!