C++ , 3ms , use vector expertly


  • 0
    B

    class Solution {
    public:
    vector<vector<int>> generate(int numRows) {
    vector<vector<int>> triangle;
    if(numRows <1)
    return triangle;
    vector<int> temp(1,1);
    triangle.push_back(temp);
    for(int i =2 ;i<=numRows;i++)
    {
    temp.push_back(0); //加一个值进来
    vector<int> cur = temp;
    for(int j = 1;j<i;j++)
    cur[j] = temp[j] + temp[j-1]; //得到每一层的vector
    temp = cur;
    triangle.push_back(temp);
    }
    return triangle;
    }
    };


Log in to reply
 

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