My C++ code, 0ms, O(N) time and space


  • 0
    M
    class Solution {
    public:
        vector<int> getRow(int rowIndex) {
            vector<int> result(rowIndex+1,0);
            result[0]=1;
            if(rowIndex)
            {
                int half=(rowIndex%2+rowIndex)/2;
                for(int i=1;i<=half;i++)
                {
                    long tmp=((long)result[i-1])*(rowIndex+1-i);
                    tmp=tmp/i;
                    result[i]=tmp;
                }
                for(int i=rowIndex;i>=half;i--)
                    result[i]=result[rowIndex-i];
            }
            return result;
        }
    };

Log in to reply
 

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