[leetcode] Pascal's Triangle II

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;
}


你可能感兴趣的:([leetcode] Pascal's Triangle II)