【easy】118.119.杨辉三角

这题必会啊!!!

第一题118.

class Solution {
public:
    vector<vector<int>> generate(int numRows) {
        vector<vector<int>> vec;  
        for(int i=0;i<numRows;i++){  
            vector<int> tmp(i+1); //这样就相当于一个数组,可以用下标了 
            tmp[0]=tmp[i]=1;  
            for(int j=1;j<i;j++){  
                tmp[j] = vec[i-1][j]+vec[i-1][j-1];  
            }  
            vec.push_back(tmp);  
        }  
        return vec;
    }
};

 

第二题119.

class Solution {
public:
    vector<int> getRow(int rowIndex) {
        vector<int> res(rowIndex+1);
        res.assign(rowIndex+1,1);
        for (int i=0;i<rowIndex;i++){
            
            for (int j=i;j>=1;j--){
                res[j] += res[j-1];
            }
        }
        return res;
    }
};

 

posted @ 2018-01-22 20:18  Sherry_Yang  阅读(93)  评论(0编辑  收藏  举报