Leetcode[118] Pascal

来源:互联网 发布:linux 添加输入法 编辑:程序博客网 时间:2024/04/30 10:35

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1.初始化numRows行(先得判定numRows是不是为0)

2.每一行的元素,特殊元素:第一个和最后一个为1

3.每一行的元素,普通元素:其余元素为上面的左边和右边

vector<vector<int> > generate(int numRows) {vector<vector<int> > res;if (numRows == 0)return res;res.resize(numRows);res[0].push_back(1);for (int i = 1; i < numRows;i++){res[i].resize(i+1);res[i][0] = res[i][i] = 1;for (int j = 1; j < i;j++){res[i][j] = res[i - 1][j-1]+res[i-1][j];}}return res;}


0 0
原创粉丝点击