码迷,mamicode.com
首页 > 其他好文 > 详细

【easy】118.119.杨辉三角

时间:2018-01-22 21:17:28      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:assign   str   gpo   个数   int   rate   pre   下标   index   

这题必会啊!!!

第一题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;
    }
};

 

【easy】118.119.杨辉三角

标签:assign   str   gpo   个数   int   rate   pre   下标   index   

原文地址:https://www.cnblogs.com/sherry-yang/p/8330942.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!