0ms C++ solution simple


  • 0
    C
    class Solution {
    public:
        vector<vector<int>> generate(int numRows) {
            
            vector<vector<int>> ret;
            vector<int> prevLay;
            int i=0, j=0;
            
            for (i=0; i<numRows; i++)
            {
                vector<int> lay;
                
                for (j=0; j<i+1; j++)
                {
                    if (j==0 || j==i)
                    {
                        lay.push_back(1);
                    }
                    else
                    {
                        int val = prevLay[j-1] + prevLay[j];
                        lay.push_back(val);
                    }
                }
                
                prevLay = lay;
                ret.emplace_back(lay);
            }
            
            return ret;
        }
    };

Log in to reply
 

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