Pascal's Triangle,Pascal's Triangle II

一.Pascal's Triangle

Given numRows, generate the first numRows of Pascal's triangle.

For example, given numRows = 5,
Return

[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> res;
if(numRows==){
return res;
}
vector<int> row;
int size = ;
while(numRows--){
int x = 1;for(int i=;i<size;i++){
int y = row[i];
row[i]= x+y;
x = y;
}
row.push_back();
res.push_back(row);
size++;
}
return res;
}
};

二.Pascal's Triangle II

Given an index k, return the kth row of the Pascal's triangle.

For example, given k = 3,
Return [1,3,3,1].

class Solution {
public:
vector<int> getRow(int rowIndex) {
rowIndex+=;
vector<int> row;
int size = ;
while(rowIndex--){
int x = ;
for(int i=;i<size;i++){
int y = row[i];
row[i] = x+y;
x=y;
}
row.push_back();
size++;
}
return row;
}
};
上一篇:Django session/cookie


下一篇:JAVA语法基础之函数的使用说明