C++ implementation


  • 5
    class Solution {
    public:
        vector<int> getRow(int rowIndex) {
            vector<int> result(rowIndex+1, 0);
            result[0]=1;
            for(int i=1; i<=rowIndex; i++){
                for(int j=i; j>=1; j--)
                    result[j]=result[j]+result[j-1];
            }
            return result;
        }
    };

  • 0
    L

    My solution is almost the same, except the vector is initialized to 1.

    class Solution {
    public:
        vector<int> getRow(int rowIndex) {
            vector<int> ret(rowIndex+1, 1);
            for(int i = 1; i <= rowIndex; i++){
                for(int j = i-1; j > 0; j--){
                    ret[j] += ret[j-1];
                }
            }
            return ret;
        }
    };

Log in to reply
 

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