BiruLyu
6/20/2017 - 5:02 AM

119. Pascal's Triangle II(1st).java

"""
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?

TESTCAES:
Input:
0
1
2
3
4
5

Output:
[1]
[1,1]
[1,2,1]
[1,3,3,1]
[1,4,6,4,1]
[1,5,10,10,5,1]
"""
class Solution(object):
    def getRow(self, rowIndex):
        """
        :type rowIndex: int
        :rtype: List[int]
        """
        res = [];
        if rowIndex == 0:
            return [1];
        
        temp = [1];
        for i in range(1,rowIndex + 1):
            res = [];
            res.append(1);
            j = 0;
            while(j < i - 1):
                res.append(temp[j] + temp[j + 1]);
                j += 1;
            res.append(1);
            temp = res;
        
        return temp;
public class Solution {
    public List<Integer> getRow(int rowIndex) {
        List<Integer> res = new ArrayList<Integer>();
        if(rowIndex < 0) return res;
        long cur = 1;
        res.add((int)cur);
        for (int i = 0; i < rowIndex; i++) {
            cur = (int)(1.0 * cur * (rowIndex - i) / (i + 1));// avoid overflowing
            res.add((int)cur);
        }
        return res;
    }
    

}
public class Solution {
    public List<Integer> getRow(int rowIndex) {
        List<Integer> res = new ArrayList<Integer>();
        if(rowIndex < 0) return res;
        long cur = 1;
        res.add((int)cur);
        for (int i = 0; i < rowIndex; i++) {
            cur = (int)(1.0 * cur * (rowIndex - i) / (i + 1));// avoid overflowing
            res.add((int)cur);
        }
        return res;
    }
    

}