【LeetCode】119.Pascal’s Triangle II(Easy)解题报告
题目地址:https://leetcode.com/problems/pascals-triangle-ii/description/
题目描述:
Given an index k, return the kth row of the Pascal’s triangle.
For example, given k = 3 , Return [1,3,3,1].
Note : Could you optimize your algorithm to use only O(k) extra space?
Solutions:
//time:O(n^2)
//space:O(n)
class Solution {
public List<Integer> getRow(int rowIndex) {
List<Integer> res = new ArrayList<>();
if(rowIndex < 0) return res;
for(int i=0 ; i<rowIndex+1 ; i++){
res.add(0,1);
for(int j=1 ; j<res.size()-1 ; j++){
res.set(j,res.get(j)+res.get(j+1));
}
}
return res;
}
}
Date:2018年2月12日