Any one can help with c++ code?


  • 0
    Y

    class Solution {
    public:

    vector<int> getRow(int rowIndex) {
        vector<int> row;
        if(rowIndex<0) return row;
        row.push_back(1);
        if(rowIndex==0) return row;
        
        vector<int> lastrow;
        lastrow.push_back(1); //last row only with 1;
        
        for(int i=1;i<=rowIndex;i++){
            row.clear();
            row.push_back(1);
            if(i>1)
            {
                for(int j=1;j<rowIndex;j++)
                {
                    row.push_back(lastrow[j-1]+lastrow[j]);
                }
            }
            row.push_back(1);
            lastrow=row;
        }
        return row;
    }
    

    };

    showing error:
    Input: 3

    Output: [1,3,4,1]

    Expected: [1,3,3,1]


  • 0
    W

    It should be "for (int j = 1; j < i; j++)" in line 15.


Log in to reply
 

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