O(k) space C++ solution, 15 lines code


  • 2
    H
    class Solution {
     vector<int> getRow(int rowIndex) {
        vector<int> vi; vi.push_back(1);
        if(rowIndex==0) return vi;
        for(int i = 1; i<=rowIndex; i++){
            for(int j = 1, a = vi[0], b; j<vi.size(); j++, a = b){
                b = vi[j];
                vi[j] = a + vi[j];
            }
            vi.push_back(1);
        }
        return vi;
    }
    };

Log in to reply
 

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