Fork me on GitHub

[leetcode-119-Pascal's Triangle II]

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?

vector<int> getRow(int rowIndex)
    {
        vector<int>result;
        vector<int>temp;
        if (rowIndex < 0)return result;
        temp.push_back(1);
        if (rowIndex == 0)return temp;
        
        for (int i = 0; i <= rowIndex;i++)
        {
            result.clear();
            result.push_back(1);
            for (int j = 1; j < i; j++)
            {
                result.push_back(temp[j - 1] + temp[j]);
            }
            result.push_back(1);
            temp = result;
        }
        return result;
    }

 

posted @ 2017-04-05 17:36  hellowOOOrld  阅读(106)  评论(0编辑  收藏  举报