c++ - "ret.push_back(vector) 是什么意思());“ 意思是?
问题描述
你能告诉我具体是什么ret.push_back(vector<int>());
意思吗?整个代码如下:
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
struct Solution
{
vector <vector <int>> ret;
void buildVector(TreeNode *root, int depth)
{
if(root==NULL) return;
if(ret.size()==depth) ret.push_back(vector<int>());
ret[depth].push_back(root->val);
buildVector(root->left,depth+1);
buildVector(root->right,depth+1);
}
vector<vector<int>> levelOrder(TreeNode *A)
{
buildVector(A,0);
return ret;
}
};
解决方案
vector <vector<int>> ret;
手段ret
是向量的向量。为了容易理解这一点,vector 是一个动态数组。所以基本上,ret
是一个数组数组。
因此,在这种情况下,ret.push_back(vector<int>())
意味着您正在向数组数组中添加一个新数组。
推荐阅读
- reactjs - 单击注册按钮时打开一个新组件
- reactjs - 图片上传预览:window.URL.createObjectURL() 只工作一次
- java - 空单元格检查规则引擎决策表
- firebase - 将列表视图中的数据打印到 Firebase
- google-bigquery - Jupyter Notebook 无法访问 Big Query
- angular - “HammerInput”类型上不存在属性“maxPointers”
- c - 缺少头文件 (ini.h)
- java - 将列表添加到一组java中
- javascript - 如何使用节点写入 json 文件?
- vue.js - Vue + Element:.docx/.pptx/.pdf 文档的查看器