您的位置:首页 > 其它

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:

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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: