LeetCode: Pascal's Triangle

思路:(好吧,这个题目是直接在网页上敲出来的,^_^,由于下标问题挂了一次,相当水的题了,当前行的元素用前一行同一列和前一列的元素相加,出了头和尾的元素为1)

class Solution {
public:
    vector<vector<int> > generate(int numRows) {
        vector<vector<int> > ret;
        if(numRows >= 1){
            vector<int> temp;
            temp.push_back(1);
            for(int i=1;i<=numRows;i++){
                vector<int> temp2(temp);
                temp.clear();
                for(int j = 0;j<i;j++){
                    if(j == 0 || j == i-1)
                        temp.push_back(1);
                    else
                        temp.push_back(temp2[j] + temp2[j-1]);
                }
                ret.push_back(temp);
            }
        }
        return ret;
    }
};


你可能感兴趣的:(LeetCode: Pascal's Triangle)