杨辉三角:
1 1
1 2 1
1 3 3 1
1 4 6 4 1
最左边和最右边是1,剩下的部分是两肩的和。#include<vector>
#include<iostream>
using namespace std;
class Solution
{
public:
Solution();
~Solution();
vector<int> getRow(int rowIndex) {
vector<int> res(1, 1);
for (int i = 1; i <= rowIndex; i++){
res.push_back(1);
for (int j = res.size() - 2; j > 0; j--){
res[j] += res[j - 1];
}
}
return res;
}
};
void main(){
Solution pascal;
vector<int> res;
res = pascal.getRow(1);
for (int i:res){
cout << i << ' ';
}
cout << endl;
system("pause");
}