My simple c++ solution ,O(k) space ,two var for last and current


  • 1
    R
    vector<int> Solution32::getRow(int rowIndex)
    {
    	if (rowIndex <0)
    	{
    		return vector<int>();
    	}
    	vector<int> row(rowIndex+1,1);
    	for (int iter = 1 ;iter< rowIndex ;iter++)
    	{
    		int last = 1; 
    		int cur = 1;
    		for (int i = 1; i< iter+1; i++)
    		{
    			cur = row[i];
    			row[i] += last;
    			last = cur;
    			
    		}
    	}
    	return row;
    }

  • 0
    P

    it can be simplified!

    vector<int> getRow(int rowIndex) {
        vector<int> ans;
        int pre, tmp;
        for (int i = 0;i <= rowIndex;++i) {
            pre = 0;
            for (int j = 0;j < i;++j) {
                tmp = ans[j];
                ans[j] += pre;
                pre = tmp;
            }
            ans.push_back(1);
        }
        return ans;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.