C++ solution..... 0ms.........


  • -1
    L

    this is the first time i hit 0 ms.... LoL ~

    class Solution {
    public:
    vector<vector<int>> result;

    void generateArrays(int theNumOftheRow) {
    vector<int> current(theNumOftheRow);
    current[0] = 1;
    current[theNumOftheRow - 1] = 1;
    if (theNumOftheRow <= 2) {
    // do nothing;
    } else {
    for (int i = 1; i<theNumOftheRow - 1; i++) {
    current[i] = (result.back())[i-1] + (result.back())[i];
    }
    }
    result.push_back(current);
    }

    vector<vector<int>> generate(int numRows) {
        for (int i = 1; i<=numRows; i++) {
            generateArrays(i);
        }
        return result;
    }
    

    };


  • 0
    B

    0ms too= =, no recursion makes our world better LOL

    class Solution {
    public:
        vector< vector<int> > generate(int numRows) {
            vector< vector<int> > pasca;
            for(int i=0;i!=numRows;i++){
            	vector<int> layer;
            	if(i==0){
    	        	layer.push_back(1);
    	        	pasca.push_back(layer);
    	        }
    	        else if(i==1){
            		layer.push_back(1);
            		layer.push_back(1);
    	        	pasca.push_back(layer);
            	}
    	        else{
    	        	layer.push_back(1);
            		for(int j=1;j!=pasca[i-1].size();j++){
    		        	layer.push_back(pasca[i-1][j-1]+pasca[i-1][j]);
    		        }
    		        layer.push_back(1);
    		        pasca.push_back(layer);
            	}
            }
            return pasca;
    		
        }
    };
    

Log in to reply
 

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