[leetcode] Pascal's Triangle II

来源:互联网 发布:打印机无法网络打印 编辑:程序博客网 时间:2024/06/06 02:50

Pascal's Triangle II


#include <iostream>#include <vector>using namespace std;class Solution {public:    vector<int> getRow(int rowIndex) {        vector<int> res(rowIndex+1);        for (int i=0; i<=rowIndex; i++) {            for (int j=i; j>=0; j--) {                if (j==0||j==i) {                    res[j]=1;                }else{                    res[j]=res[j-1]+res[j];//update                }            }        }        /*for (int i=0; i<rowIndex; i++) {            cout<<res[i]<<" ";        }        cout<<res[rowIndex]<<endl;*/        return  res;    }};int main(){    Solution so;    int n=4;    so.getRow(n);    return 0;}


0 0
原创粉丝点击