[leetcode]Pascal's Triangle

class Solution {
public:
    vector<vector<int> > generate(int numRows) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
        vector<vector<int> > ans;
        if(numRows == 0) return ans;
        vector<int> tmp ; tmp.push_back(1);
        ans.push_back(tmp);
        for(int i = 1 ; i < numRows ; i++){
          //  cout << i << endl;
            int element = i + 1;
            vector<int> x(element);
            x[0] = 1 ; x[element-1] = 1;
            for(int j = 1 ; j < element - 1 ; j++)
                x[j] = ans[i-1][j-1] + ans[i-1][j];
            ans.push_back(x);
        }
        return ans;
    }
};

 

你可能感兴趣的:(LeetCode)