C++/Java concise solution using O(k) space.


  • 0
    C

    C++ version:

    vector<int> getRow(int rowIndex) {
        vector<int> ret = {};
        for (int i=0; i<rowIndex+1; i++) {
            ret.push_back(1);
            for (int j=i-1; j>0; j--)  //update reversely
                ret[j] += ret[j-1];
        }
        return ret;
    }   
    

    Java version:

    public List<Integer> getRow(int rowIndex) {
        List<Integer> ret = new LinkedList<Integer>();
        for (int i=0; i<rowIndex+1; i++) {
            ret.add(0, 1);  //insert at the beginning
            for (int j=1; j<i; j++)
                ret.set(j, ret.get(j)+ret.get(j+1));
        }
        return ret;
    }

Log in to reply
 

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