vector题目
118. 杨辉三角 - 力扣(LeetCode)
求解题目时候,我们可以将其看作近似的二维数组。
行为vector<vector<int>>,数组的每个数据类型为vector<int>。
列为vector<int>,数组的每个数据类型为int。
通过观察我们可以发现其关系为(定义vector<vector<int>> = vv; 行为 i ,列为 j ),
vv[ i ][ j ] = vv[ i - 1 ][ j ] + vv[ i - 1 ][ j - 1 ]
class Solution {
public:vector<vector<int>> generate(int numRows) {vector<vector<int>> vv;vv.resize(numRows);for (int i = 0; i < vv.size(); i++){vv[i].resize(i + 1, 0);vv[i][0] = vv[i][i] = 1;}for (int i = 0; i < vv.size(); i++){for(int j = 0; j < vv[i].size(); j++){if (vv[i][j] == 0){vv[i][j] = vv[i-1][j] + vv[i-1][j-1];}}}return vv;}
};