My very simple C++ solution


  • 8
    Z

    Basically, I will insert 1 at the head of vector each iteration and calculate result[j] based on result[j] and result[j+1].
    For example, if I want to get [1, 3, 3, 1] from [1, 2, 1], I will first insert 1 and get [1, 1, 2, 1].
    Then begin with j = 1, result[j] = result[j] + result[j+1]. second position is 1+2=3, third position is 2+1=3
    Finally we get [1, 3, 3, 1]

    #include <vector>
    using namespace std;
    class Solution {
    public:
        vector<int> getRow(int rowIndex) {
            vector<int> result(1,1);
            for(int i=1;i<=rowIndex;i++){
                result.insert(result.begin(),1);
                for(int j=1;j<result.size()-1;j++){
                    result[j] = result[j] + result[j+1];
                }
            }
            return result;
        }
    };

  • 9
    V

    A very Slight modification; instead of insert, I am making use of symmetry and pushing back at the end.

    Only difference it makes is that insert is inefficient, the latter solution perhaps improves efficiency!

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

  • 0
    L

    very smart solution


Log in to reply
 

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