提交 75a7f510 编写于 作者: 每日一练社区's avatar 每日一练社区

add exercises

上级 66b8b16b
{
"node_id": "algorithm-7d5924a413584b319285481e9745c0d9",
"keywords": [
"leetcode",
"二叉树的序列化与反序列化"
],
"children": [],
"export": [
"solution.json"
],
"title": "二叉树的序列化与反序列化"
}
\ No newline at end of file
<p>序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据。</p>
<p>请设计一个算法来实现二叉树的序列化与反序列化。这里不限定你的序列 / 反序列化算法执行逻辑,你只需要保证一个二叉树可以被序列化为一个字符串并且将这个字符串反序列化为原始的树结构。</p>
<p><strong>提示: </strong>输入输出格式与 LeetCode 目前使用的方式一致,详情请参阅 <a href="/faq/#binary-tree">LeetCode 序列化二叉树的格式</a>。你并非必须采取这种方式,你也可以采用其他的方法解决这个问题。</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/15/serdeser.jpg" style="width: 442px; height: 324px;" />
<pre>
<strong>输入:</strong>root = [1,2,3,null,null,4,5]
<strong>输出:</strong>[1,2,3,null,null,4,5]
</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong>root = []
<strong>输出:</strong>[]
</pre>
<p><strong>示例 3:</strong></p>
<pre>
<strong>输入:</strong>root = [1]
<strong>输出:</strong>[1]
</pre>
<p><strong>示例 4:</strong></p>
<pre>
<strong>输入:</strong>root = [1,2]
<strong>输出:</strong>[1,2]
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li>树中结点数在范围 <code>[0, 10<sup>4</sup>]</code></li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "1cdb3535a5ae494eaeca728a1b141ae1"
}
\ No newline at end of file
# 二叉树的序列化与反序列化
<p>序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据。</p>
<p>请设计一个算法来实现二叉树的序列化与反序列化。这里不限定你的序列 / 反序列化算法执行逻辑,你只需要保证一个二叉树可以被序列化为一个字符串并且将这个字符串反序列化为原始的树结构。</p>
<p><strong>提示: </strong>输入输出格式与 LeetCode 目前使用的方式一致,详情请参阅 <a href="/faq/#binary-tree">LeetCode 序列化二叉树的格式</a>。你并非必须采取这种方式,你也可以采用其他的方法解决这个问题。</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/15/serdeser.jpg" style="width: 442px; height: 324px;" />
<pre>
<strong>输入:</strong>root = [1,2,3,null,null,4,5]
<strong>输出:</strong>[1,2,3,null,null,4,5]
</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong>root = []
<strong>输出:</strong>[]
</pre>
<p><strong>示例 3:</strong></p>
<pre>
<strong>输入:</strong>root = [1]
<strong>输出:</strong>[1]
</pre>
<p><strong>示例 4:</strong></p>
<pre>
<strong>输入:</strong>root = [1,2]
<strong>输出:</strong>[1,2]
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li>树中结点数在范围 <code>[0, 10<sup>4</sup>]</code></li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
<p>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
```
### after
```cpp
```
## 答案
```cpp
class Codec
{
public:
string serialize(TreeNode *root)
{
if (!root)
return "null ";
return serialize(root->left) + serialize(root->right) + to_string(root->val);
}
TreeNode *helptrans(istringstream &ss)
{
string s;
TreeNode *root = NULL;
ss >> s;
if (s == "null")
return root;
root = new TreeNode(stoi(s));
root->right = helptrans(ss);
root->left = helptrans(ss);
return root;
}
TreeNode *deserialize(string data)
{
stringstream ss;
ss << data;
string s, trans;
while (ss >> s)
trans = s + " " + trans;
istringstream st(trans);
return helptrans(st);
}
};
```
## 选项
### A
```cpp
class Codec
{
public:
string serialize(TreeNode *root)
{
if (!root)
return "#_";
return to_string(root->val) + "_" + serialize(root->left) + serialize(root->right);
}
TreeNode *deserialize(string data)
{
cout << data << endl;
queue<string> q;
stringstream ss(data);
string s;
while (getline(ss, s, '_'))
q.push(s);
return help(q);
}
TreeNode *help(queue<string> &q)
{
auto cur = q.front();
q.pop();
if (cur == "#")
return NULL;
auto root = new TreeNode(stoi(cur));
root->left = help(q);
root->right = help(q);
return root;
}
};
```
### B
```cpp
class Codec
{
public:
string serialize(TreeNode *root)
{
string ans;
if (root == NULL)
return "[]";
ans = "[";
queue<TreeNode *> que;
que.push(root);
char str[100];
while (!que.empty())
{
TreeNode *top = que.front();
que.pop();
if (top != NULL)
{
que.push(top->left);
que.push(top->right);
sprintf(str, "%d", top->val);
ans += str;
}
else
{
ans += "null";
}
ans += ",";
}
int end = ans.length() - 1;
while (!(ans[end] >= '0' && ans[end] <= '9'))
end--;
string rs = ans.substr(0, end + 1);
rs += "]";
return rs;
}
TreeNode *deserialize(string data)
{
int len = data.size();
if (len <= 2)
return NULL;
int numsCount = 0;
vector<TreeNode *> nums;
string word = "";
for (int i = 1; i <= len - 2; i++)
{
if (data[i] == ',')
{
TreeNode *tmp = NULL;
if (word == "null")
{
}
else
{
int num = atoi(word.c_str());
tmp = new TreeNode(num);
}
nums.push_back(tmp);
word = "";
}
else
{
word += data[i];
}
}
if (word != "" && word != "null")
{
int num = atoi(word.c_str());
TreeNode *tmp = new TreeNode(num);
nums.push_back(tmp);
}
int cnt = nums.size();
int q = 0;
int p = 1;
while (p < cnt)
{
if (nums[q] == NULL)
{
q++;
}
else
{
if (p < cnt)
nums[q]->left = nums[p];
if (p + 1 < cnt)
nums[q]->right = nums[p + 1];
p += 2;
q++;
}
}
return nums[0];
}
};
```
### C
```cpp
class Codec
{
public:
string serialize(TreeNode *root)
{
string result = "[";
queue<TreeNode *> myQue;
myQue.push(root);
while (!myQue.empty())
{
root = myQue.front();
myQue.pop();
if (root == NULL)
{
result += "null,";
continue;
}
else
{
result += to_string(root->val) + ",";
myQue.push(root->left);
myQue.push(root->right);
}
}
if (result == "[null,")
{
result.resize(result.size() - 1);
}
else
{
int endIndex = result.size() - 1;
while (result[endIndex] < '0' || result[endIndex] > '9')
{
endIndex -= 1;
}
result.resize(endIndex + 1);
}
result += "]";
return result;
}
TreeNode *deserialize(string data)
{
vector<string> dataVec;
int dataSize = data.size();
for (int index = 1; index < dataSize - 1; ++index)
{
string tempData = "";
while (index < dataSize - 1 && data[index] != ',')
{
tempData += data[index++];
}
dataVec.push_back(tempData);
}
int dataVecSize = dataVec.size();
queue<TreeNode *> myQue;
if (dataVec[0] == "null")
{
return NULL;
}
TreeNode *result = new TreeNode(atoi(dataVec[0].c_str())), *tempPtr;
myQue.push(result);
for (int index = 1; index < dataVecSize; ++index)
{
tempPtr = myQue.front();
myQue.pop();
if (dataVec[index] != "null")
{
tempPtr->left = new TreeNode(atoi(dataVec[index].c_str()));
myQue.push(tempPtr->left);
}
index += 1;
if (index < dataVecSize && dataVec[index] != "null")
{
tempPtr->right = new TreeNode(atoi(dataVec[index].c_str()));
myQue.push(tempPtr->right);
}
}
return result;
}
};
```
{
"node_id": "algorithm-b52188ca107f42db935096b8640aa995",
"keywords": [
"leetcode",
"扁平化嵌套列表迭代器"
],
"children": [],
"export": [
"solution.json"
],
"title": "扁平化嵌套列表迭代器"
}
\ No newline at end of file
<p>给你一个嵌套的整数列表 <code>nestedList</code> 。每个元素要么是一个整数,要么是一个列表;该列表的元素也可能是整数或者是其他列表。请你实现一个迭代器将其扁平化,使之能够遍历这个列表中的所有整数。</p>
<p>实现扁平迭代器类 <code>NestedIterator</code></p>
<ul>
<li><code>NestedIterator(List&lt;NestedInteger&gt; nestedList)</code> 用嵌套列表 <code>nestedList</code> 初始化迭代器。</li>
<li><code>int next()</code> 返回嵌套列表的下一个整数。</li>
<li><code>boolean hasNext()</code> 如果仍然存在待迭代的整数,返回 <code>true</code> ;否则,返回 <code>false</code></li>
</ul>
<p>你的代码将会用下述伪代码检测:</p>
<pre>
initialize iterator with nestedList
res = []
while iterator.hasNext()
append iterator.next() to the end of res
return res</pre>
<p>如果 <code>res</code> 与预期的扁平化列表匹配,那么你的代码将会被判为正确。</p>
<p>&nbsp;</p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong>nestedList = [[1,1],2,[1,1]]
<strong>输出:</strong>[1,1,2,1,1]
<strong>解释:</strong>通过重复调用&nbsp;<em>next </em>直到&nbsp;<em>hasNex</em>t 返回 false,<em>next&nbsp;</em>返回的元素的顺序应该是: <code>[1,1,2,1,1]</code></pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong>nestedList = [1,[4,[6]]]
<strong>输出:</strong>[1,4,6]
<strong>解释:</strong>通过重复调用&nbsp;<em>next&nbsp;</em>直到&nbsp;<em>hasNex</em>t 返回 false,<em>next&nbsp;</em>返回的元素的顺序应该是: <code>[1,4,6]</code>
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nestedList.length &lt;= 500</code></li>
<li>嵌套列表中的整数值在范围 <code>[-10<sup>6</sup>, 10<sup>6</sup>]</code></li>
</ul>
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "29acba1430844966b2c998a2e2c82649"
}
\ No newline at end of file
# 扁平化嵌套列表迭代器
<p>给你一个嵌套的整数列表 <code>nestedList</code> 。每个元素要么是一个整数,要么是一个列表;该列表的元素也可能是整数或者是其他列表。请你实现一个迭代器将其扁平化,使之能够遍历这个列表中的所有整数。</p>
<p>实现扁平迭代器类 <code>NestedIterator</code></p>
<ul>
<li><code>NestedIterator(List&lt;NestedInteger&gt; nestedList)</code> 用嵌套列表 <code>nestedList</code> 初始化迭代器。</li>
<li><code>int next()</code> 返回嵌套列表的下一个整数。</li>
<li><code>boolean hasNext()</code> 如果仍然存在待迭代的整数,返回 <code>true</code> ;否则,返回 <code>false</code></li>
</ul>
<p>你的代码将会用下述伪代码检测:</p>
<pre>
initialize iterator with nestedList
res = []
while iterator.hasNext()
append iterator.next() to the end of res
return res</pre>
<p>如果 <code>res</code> 与预期的扁平化列表匹配,那么你的代码将会被判为正确。</p>
<p>&nbsp;</p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong>nestedList = [[1,1],2,[1,1]]
<strong>输出:</strong>[1,1,2,1,1]
<strong>解释:</strong>通过重复调用&nbsp;<em>next </em>直到&nbsp;<em>hasNex</em>t 返回 false,<em>next&nbsp;</em>返回的元素的顺序应该是: <code>[1,1,2,1,1]</code></pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong>nestedList = [1,[4,[6]]]
<strong>输出:</strong>[1,4,6]
<strong>解释:</strong>通过重复调用&nbsp;<em>next&nbsp;</em>直到&nbsp;<em>hasNex</em>t 返回 false,<em>next&nbsp;</em>返回的元素的顺序应该是: <code>[1,4,6]</code>
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nestedList.length &lt;= 500</code></li>
<li>嵌套列表中的整数值在范围 <code>[-10<sup>6</sup>, 10<sup>6</sup>]</code></li>
</ul>
<p>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
// This is the interface that allows for creating nested lists.
// You should not implement it, or speculate about its implementation
class NestedInteger
{
public:
// Return true if this NestedInteger holds a single integer, rather than a nested list.
bool isInteger() const;
// Return the single integer that this NestedInteger holds, if it holds a single integer
// The result is undefined if this NestedInteger holds a nested list
int getInteger() const;
// Return the nested list that this NestedInteger holds, if it holds a nested list
// The result is undefined if this NestedInteger holds a single integer
const vector<NestedInteger> &getList() const;
};
```
### after
```cpp
```
## 答案
```cpp
class NestedIterator
{
public:
vector<int> data;
vector<int>::iterator it;
NestedIterator(vector<NestedInteger> &nestedList)
{
parse(nestedList);
it = data.begin();
}
void parse(vector<NestedInteger> &nestedList)
{
for (auto nes : nestedList)
{
data.push_back(nes.getInteger());
parse(nes.getList());
}
}
int next()
{
return *it++;
}
bool hasNext()
{
return it != data.end();
}
};
```
## 选项
### A
```cpp
class NestedIterator
{
public:
stack<NestedInteger *> st;
NestedIterator(vector<NestedInteger> &nestedList)
{
for (int i = nestedList.size() - 1; i >= 0; --i)
st.push(&nestedList[i]);
}
int next()
{
int res = st.top()->getInteger();
st.pop();
return res;
}
bool hasNext()
{
if (st.empty())
return false;
NestedInteger *tmp = st.top();
while (!tmp->isInteger())
{
st.pop();
vector<NestedInteger> &list = tmp->getList();
for (int i = list.size() - 1; i >= 0; --i)
st.push(&list[i]);
if (st.empty())
return false;
tmp = st.top();
}
return true;
}
};
```
### B
```cpp
class NestedIterator
{
public:
int cnt = 0;
vector<int> ans;
void dfs(vector<NestedInteger> &nestedList)
{
for (auto l : nestedList)
{
if (l.isInteger())
ans.push_back(l.getInteger());
else
dfs(l.getList());
}
}
NestedIterator(vector<NestedInteger> &nestedList)
{
dfs(nestedList);
}
int next()
{
return ans[cnt++];
}
bool hasNext()
{
return cnt < ans.size();
}
};
```
### C
```cpp
class NestedIterator
{
stack<vector<NestedInteger>::iterator> begins, ends;
public:
NestedIterator(vector<NestedInteger> &nestedList)
{
begins.push(nestedList.begin());
ends.push(nestedList.end());
}
int next()
{
hasNext();
return (begins.top()++)->getInteger();
}
bool hasNext()
{
vector<NestedInteger>::iterator tp;
while (!begins.empty())
{
if (begins.top() == ends.top())
{
begins.pop();
ends.pop();
}
else
{
tp = begins.top();
if (tp->isInteger())
return true;
begins.top()++;
begins.push(tp->getList().begin());
ends.push(tp->getList().end());
;
}
}
return false;
}
};
```
...@@ -45,10 +45,22 @@ ...@@ -45,10 +45,22 @@
</ul> </ul>
<p>以下错误的选项是?</p> <p>以下错误的选项是?</p>
## aop ## aop
### before ### before
```cpp ```cpp
#include <bits/stdc++.h>
using namespace std;
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
``` ```
### after ### after
```cpp ```cpp
...@@ -57,21 +69,285 @@ ...@@ -57,21 +69,285 @@
## 答案 ## 答案
```cpp ```cpp
class Codec
{
public:
string serialize(TreeNode *root)
{
if (!root)
return "null ";
return serialize(root->left) + serialize(root->right) + to_string(root->val);
}
TreeNode *helptrans(istringstream &ss)
{
string s;
TreeNode *root = NULL;
ss >> s;
if (s == "null")
return root;
root = new TreeNode(stoi(s));
root->right = helptrans(ss);
root->left = helptrans(ss);
return root;
}
TreeNode *deserialize(string data)
{
stringstream ss;
ss << data;
string s, trans;
while (ss >> s)
trans = s + " " + trans;
istringstream st(trans);
return helptrans(st);
}
};
``` ```
## 选项 ## 选项
### A ### A
```cpp ```cpp
class Codec
{
public:
string serialize(TreeNode *root)
{
if (!root)
return "#_";
return to_string(root->val) + "_" + serialize(root->left) + serialize(root->right);
}
TreeNode *deserialize(string data)
{
cout << data << endl;
queue<string> q;
stringstream ss(data);
string s;
while (getline(ss, s, '_'))
q.push(s);
return help(q);
}
TreeNode *help(queue<string> &q)
{
auto cur = q.front();
q.pop();
if (cur == "#")
return NULL;
auto root = new TreeNode(stoi(cur));
root->left = help(q);
root->right = help(q);
return root;
}
};
``` ```
### B ### B
```cpp ```cpp
class Codec
{
public:
string serialize(TreeNode *root)
{
string ans;
if (root == NULL)
return "[]";
ans = "[";
queue<TreeNode *> que;
que.push(root);
char str[100];
while (!que.empty())
{
TreeNode *top = que.front();
que.pop();
if (top != NULL)
{
que.push(top->left);
que.push(top->right);
sprintf(str, "%d", top->val);
ans += str;
}
else
{
ans += "null";
}
ans += ",";
}
int end = ans.length() - 1;
while (!(ans[end] >= '0' && ans[end] <= '9'))
end--;
string rs = ans.substr(0, end + 1);
rs += "]";
return rs;
}
TreeNode *deserialize(string data)
{
int len = data.size();
if (len <= 2)
return NULL;
int numsCount = 0;
vector<TreeNode *> nums;
string word = "";
for (int i = 1; i <= len - 2; i++)
{
if (data[i] == ',')
{
TreeNode *tmp = NULL;
if (word == "null")
{
}
else
{
int num = atoi(word.c_str());
tmp = new TreeNode(num);
}
nums.push_back(tmp);
word = "";
}
else
{
word += data[i];
}
}
if (word != "" && word != "null")
{
int num = atoi(word.c_str());
TreeNode *tmp = new TreeNode(num);
nums.push_back(tmp);
}
int cnt = nums.size();
int q = 0;
int p = 1;
while (p < cnt)
{
if (nums[q] == NULL)
{
q++;
}
else
{
if (p < cnt)
nums[q]->left = nums[p];
if (p + 1 < cnt)
nums[q]->right = nums[p + 1];
p += 2;
q++;
}
}
return nums[0];
}
};
``` ```
### C ### C
```cpp ```cpp
class Codec
{
public:
string serialize(TreeNode *root)
{
string result = "[";
queue<TreeNode *> myQue;
myQue.push(root);
while (!myQue.empty())
{
root = myQue.front();
myQue.pop();
if (root == NULL)
{
result += "null,";
continue;
}
else
{
result += to_string(root->val) + ",";
myQue.push(root->left);
myQue.push(root->right);
}
}
if (result == "[null,")
{
result.resize(result.size() - 1);
}
else
{
int endIndex = result.size() - 1;
while (result[endIndex] < '0' || result[endIndex] > '9')
{
endIndex -= 1;
}
result.resize(endIndex + 1);
}
result += "]";
return result;
}
TreeNode *deserialize(string data)
{
vector<string> dataVec;
int dataSize = data.size();
for (int index = 1; index < dataSize - 1; ++index)
{
string tempData = "";
while (index < dataSize - 1 && data[index] != ',')
{
tempData += data[index++];
}
dataVec.push_back(tempData);
}
int dataVecSize = dataVec.size();
queue<TreeNode *> myQue;
if (dataVec[0] == "null")
{
return NULL;
}
TreeNode *result = new TreeNode(atoi(dataVec[0].c_str())), *tempPtr;
myQue.push(result);
for (int index = 1; index < dataVecSize; ++index)
{
tempPtr = myQue.front();
myQue.pop();
if (dataVec[index] != "null")
{
tempPtr->left = new TreeNode(atoi(dataVec[index].c_str()));
myQue.push(tempPtr->left);
}
index += 1;
if (index < dataVecSize && dataVec[index] != "null")
{
tempPtr->right = new TreeNode(atoi(dataVec[index].c_str()));
myQue.push(tempPtr->right);
}
}
return result;
}
};
``` ```
...@@ -47,10 +47,29 @@ return res</pre> ...@@ -47,10 +47,29 @@ return res</pre>
</ul> </ul>
<p>以下错误的选项是?</p> <p>以下错误的选项是?</p>
## aop ## aop
### before ### before
```cpp ```cpp
#include <bits/stdc++.h>
using namespace std;
// This is the interface that allows for creating nested lists.
// You should not implement it, or speculate about its implementation
class NestedInteger
{
public:
// Return true if this NestedInteger holds a single integer, rather than a nested list.
bool isInteger() const;
// Return the single integer that this NestedInteger holds, if it holds a single integer
// The result is undefined if this NestedInteger holds a nested list
int getInteger() const;
// Return the nested list that this NestedInteger holds, if it holds a nested list
// The result is undefined if this NestedInteger holds a single integer
const vector<NestedInteger> &getList() const;
};
``` ```
### after ### after
```cpp ```cpp
...@@ -59,21 +78,159 @@ return res</pre> ...@@ -59,21 +78,159 @@ return res</pre>
## 答案 ## 答案
```cpp ```cpp
class NestedIterator
{
public:
vector<int> data;
vector<int>::iterator it;
NestedIterator(vector<NestedInteger> &nestedList)
{
parse(nestedList);
it = data.begin();
}
void parse(vector<NestedInteger> &nestedList)
{
for (auto nes : nestedList)
{
data.push_back(nes.getInteger());
parse(nes.getList());
}
}
int next()
{
return *it++;
}
bool hasNext()
{
return it != data.end();
}
};
``` ```
## 选项 ## 选项
### A ### A
```cpp ```cpp
class NestedIterator
{
public:
stack<NestedInteger *> st;
NestedIterator(vector<NestedInteger> &nestedList)
{
for (int i = nestedList.size() - 1; i >= 0; --i)
st.push(&nestedList[i]);
}
int next()
{
int res = st.top()->getInteger();
st.pop();
return res;
}
bool hasNext()
{
if (st.empty())
return false;
NestedInteger *tmp = st.top();
while (!tmp->isInteger())
{
st.pop();
vector<NestedInteger> &list = tmp->getList();
for (int i = list.size() - 1; i >= 0; --i)
st.push(&list[i]);
if (st.empty())
return false;
tmp = st.top();
}
return true;
}
};
``` ```
### B ### B
```cpp ```cpp
class NestedIterator
{
public:
int cnt = 0;
vector<int> ans;
void dfs(vector<NestedInteger> &nestedList)
{
for (auto l : nestedList)
{
if (l.isInteger())
ans.push_back(l.getInteger());
else
dfs(l.getList());
}
}
NestedIterator(vector<NestedInteger> &nestedList)
{
dfs(nestedList);
}
int next()
{
return ans[cnt++];
}
bool hasNext()
{
return cnt < ans.size();
}
};
``` ```
### C ### C
```cpp ```cpp
class NestedIterator
{
stack<vector<NestedInteger>::iterator> begins, ends;
public:
NestedIterator(vector<NestedInteger> &nestedList)
{
begins.push(nestedList.begin());
ends.push(nestedList.end());
}
int next()
{
hasNext();
return (begins.top()++)->getInteger();
}
bool hasNext()
{
vector<NestedInteger>::iterator tp;
while (!begins.empty())
{
if (begins.top() == ends.top())
{
begins.pop();
ends.pop();
}
else
{
tp = begins.top();
if (tp->isInteger())
return true;
begins.top()++;
begins.push(tp->getList().begin());
ends.push(tp->getList().end());
;
}
}
return false;
}
};
``` ```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册