Pascal’s Triangle II, Leetcode 解题笔记

Given an index k, return the kth row of the Pascal’s triangle.

For example, given k = 3,
Return [1,3,3,1].

Could you optimize your algorithm to use only O(k) extra space?


public class Solution {
    public ArrayList<Integer> getRow(int rowIndex) {
        ArrayList<Integer> ret = new ArrayList<Integer>();
        if(rowIndex == 0) return ret;
        for(int i = 0; i < rowIndex; i++){
            int len = ret.size();
            for(int j = len-1; j > 0; j--){
                ret.set(j, ret.get(j-1) + ret.get(j));
        return ret;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s