C++ another idea


  • 0
    B

    to get the n-th row, we can put n-1 th row in the left n elements of the vector, then start to update from right end. Next round the result is in the right end of vector, so we can start update from left.

    class Solution {
    public:
        vector<int> getRow(int rowIndex) 
        {
            vector<int> ans(rowIndex+1,1);
            int left = 1;
            for(int i = 2; i <= rowIndex; i++ )
            {
                if(left>0) 
                {
                    for(int j = rowIndex-1; j>rowIndex-i; j--) ans[j]=ans[i+j-rowIndex]+ans[i+j-rowIndex-1];
                    ans[rowIndex-i]=1;
                }
                else 
                {
                    for(int j=1; j<i; j++) ans[j]=ans[rowIndex-i+j]+ans[rowIndex-i+j+1];
                    ans[i]=1;
                }
                left *= -1;
            }
            return ans;
        }
    };

Log in to reply
 

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