Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
CSDN 技术社区
skill_tree_algorithm
提交
75a7f510
S
skill_tree_algorithm
项目概览
CSDN 技术社区
/
skill_tree_algorithm
通知
9
Star
8
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
skill_tree_algorithm
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
75a7f510
编写于
11月 09, 2021
作者:
每日一练社区
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add exercises
上级
66b8b16b
变更
12
显示空白变更内容
内联
并排
Showing
12 changed file
with
1157 addition
and
5 deletion
+1157
-5
data/3.算法高阶/5.leetcode-设计/10.297-二叉树的序列化与反序列化/config.json
data/3.算法高阶/5.leetcode-设计/10.297-二叉树的序列化与反序列化/config.json
+13
-0
data/3.算法高阶/5.leetcode-设计/10.297-二叉树的序列化与反序列化/desc.html
data/3.算法高阶/5.leetcode-设计/10.297-二叉树的序列化与反序列化/desc.html
+44
-0
data/3.算法高阶/5.leetcode-设计/10.297-二叉树的序列化与反序列化/solution.cpp
data/3.算法高阶/5.leetcode-设计/10.297-二叉树的序列化与反序列化/solution.cpp
+0
-0
data/3.算法高阶/5.leetcode-设计/10.297-二叉树的序列化与反序列化/solution.json
data/3.算法高阶/5.leetcode-设计/10.297-二叉树的序列化与反序列化/solution.json
+7
-0
data/3.算法高阶/5.leetcode-设计/10.297-二叉树的序列化与反序列化/solution.md
data/3.算法高阶/5.leetcode-设计/10.297-二叉树的序列化与反序列化/solution.md
+353
-0
data/3.算法高阶/5.leetcode-设计/9.341-扁平化嵌套列表迭代器/config.json
data/3.算法高阶/5.leetcode-设计/9.341-扁平化嵌套列表迭代器/config.json
+13
-0
data/3.算法高阶/5.leetcode-设计/9.341-扁平化嵌套列表迭代器/desc.html
data/3.算法高阶/5.leetcode-设计/9.341-扁平化嵌套列表迭代器/desc.html
+46
-0
data/3.算法高阶/5.leetcode-设计/9.341-扁平化嵌套列表迭代器/solution.cpp
data/3.算法高阶/5.leetcode-设计/9.341-扁平化嵌套列表迭代器/solution.cpp
+0
-0
data/3.算法高阶/5.leetcode-设计/9.341-扁平化嵌套列表迭代器/solution.json
data/3.算法高阶/5.leetcode-设计/9.341-扁平化嵌套列表迭代器/solution.json
+7
-0
data/3.算法高阶/5.leetcode-设计/9.341-扁平化嵌套列表迭代器/solution.md
data/3.算法高阶/5.leetcode-设计/9.341-扁平化嵌套列表迭代器/solution.md
+236
-0
data_backup/1.leetcode/297-二叉树的序列化与反序列化/solution.md
data_backup/1.leetcode/297-二叉树的序列化与反序列化/solution.md
+276
-0
data_backup/1.leetcode/341-扁平化嵌套列表迭代器/solution.md
data_backup/1.leetcode/341-扁平化嵌套列表迭代器/solution.md
+162
-5
未找到文件。
data/3.算法高阶/5.leetcode-设计/10.297-二叉树的序列化与反序列化/config.json
0 → 100644
浏览文件 @
75a7f510
{
"node_id"
:
"algorithm-7d5924a413584b319285481e9745c0d9"
,
"keywords"
:
[
"leetcode"
,
"二叉树的序列化与反序列化"
],
"children"
:
[],
"export"
:
[
"solution.json"
],
"title"
:
"二叉树的序列化与反序列化"
}
\ No newline at end of file
data/3.算法高阶/5.leetcode-设计/10.297-二叉树的序列化与反序列化/desc.html
0 → 100644
浏览文件 @
75a7f510
<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>
data/3.算法高阶/5.leetcode-设计/10.297-二叉树的序列化与反序列化/solution.cpp
0 → 100644
浏览文件 @
75a7f510
data/3.算法高阶/5.leetcode-设计/10.297-二叉树的序列化与反序列化/solution.json
0 → 100644
浏览文件 @
75a7f510
{
"type"
:
"code_options"
,
"author"
:
"CSDN.net"
,
"source"
:
"solution.md"
,
"exercise_id"
:
"1cdb3535a5ae494eaeca728a1b141ae1"
}
\ No newline at end of file
data/3.算法高阶/5.leetcode-设计/10.297-二叉树的序列化与反序列化/solution.md
0 → 100644
浏览文件 @
75a7f510
# 二叉树的序列化与反序列化
<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
;
}
};
```
data/3.算法高阶/5.leetcode-设计/9.341-扁平化嵌套列表迭代器/config.json
0 → 100644
浏览文件 @
75a7f510
{
"node_id"
:
"algorithm-b52188ca107f42db935096b8640aa995"
,
"keywords"
:
[
"leetcode"
,
"扁平化嵌套列表迭代器"
],
"children"
:
[],
"export"
:
[
"solution.json"
],
"title"
:
"扁平化嵌套列表迭代器"
}
\ No newline at end of file
data/3.算法高阶/5.leetcode-设计/9.341-扁平化嵌套列表迭代器/desc.html
0 → 100644
浏览文件 @
75a7f510
<p>
给你一个嵌套的整数列表
<code>
nestedList
</code>
。每个元素要么是一个整数,要么是一个列表;该列表的元素也可能是整数或者是其他列表。请你实现一个迭代器将其扁平化,使之能够遍历这个列表中的所有整数。
</p>
<p>
实现扁平迭代器类
<code>
NestedIterator
</code>
:
</p>
<ul>
<li><code>
NestedIterator(List
<
NestedInteger
>
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>
</p>
<p><strong>
示例 1:
</strong></p>
<pre>
<strong>
输入:
</strong>
nestedList = [[1,1],2,[1,1]]
<strong>
输出:
</strong>
[1,1,2,1,1]
<strong>
解释:
</strong>
通过重复调用
<em>
next
</em>
直到
<em>
hasNex
</em>
t 返回 false,
<em>
next
</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>
通过重复调用
<em>
next
</em>
直到
<em>
hasNex
</em>
t 返回 false,
<em>
next
</em>
返回的元素的顺序应该是:
<code>
[1,4,6]
</code>
。
</pre>
<p>
</p>
<p><strong>
提示:
</strong></p>
<ul>
<li><code>
1
<
= nestedList.length
<
= 500
</code></li>
<li>
嵌套列表中的整数值在范围
<code>
[-10
<sup>
6
</sup>
, 10
<sup>
6
</sup>
]
</code>
内
</li>
</ul>
data/3.算法高阶/5.leetcode-设计/9.341-扁平化嵌套列表迭代器/solution.cpp
0 → 100644
浏览文件 @
75a7f510
data/3.算法高阶/5.leetcode-设计/9.341-扁平化嵌套列表迭代器/solution.json
0 → 100644
浏览文件 @
75a7f510
{
"type"
:
"code_options"
,
"author"
:
"CSDN.net"
,
"source"
:
"solution.md"
,
"exercise_id"
:
"29acba1430844966b2c998a2e2c82649"
}
\ No newline at end of file
data/3.算法高阶/5.leetcode-设计/9.341-扁平化嵌套列表迭代器/solution.md
0 → 100644
浏览文件 @
75a7f510
# 扁平化嵌套列表迭代器
<p>
给你一个嵌套的整数列表
<code>
nestedList
</code>
。每个元素要么是一个整数,要么是一个列表;该列表的元素也可能是整数或者是其他列表。请你实现一个迭代器将其扁平化,使之能够遍历这个列表中的所有整数。
</p>
<p>
实现扁平迭代器类
<code>
NestedIterator
</code>
:
</p>
<ul>
<li><code>
NestedIterator(List
<
NestedInteger
>
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>
</p>
<p><strong>
示例 1:
</strong></p>
<pre>
<strong>
输入:
</strong>
nestedList = [[1,1],2,[1,1]]
<strong>
输出:
</strong>
[1,1,2,1,1]
<strong>
解释:
</strong>
通过重复调用
<em>
next
</em>
直到
<em>
hasNex
</em>
t 返回 false,
<em>
next
</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>
通过重复调用
<em>
next
</em>
直到
<em>
hasNex
</em>
t 返回 false,
<em>
next
</em>
返回的元素的顺序应该是:
<code>
[1,4,6]
</code>
。
</pre>
<p>
</p>
<p><strong>
提示:
</strong></p>
<ul>
<li><code>
1
<
= nestedList.length
<
= 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
;
}
};
```
data_backup/1.leetcode/297-二叉树的序列化与反序列化/solution.md
浏览文件 @
75a7f510
...
...
@@ -45,10 +45,22 @@
</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
...
...
@@ -57,21 +69,285 @@
## 答案
```
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
;
}
};
```
data_backup/1.leetcode/341-扁平化嵌套列表迭代器/solution.md
浏览文件 @
75a7f510
...
...
@@ -47,10 +47,29 @@ return res</pre>
</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
...
...
@@ -59,21 +78,159 @@ return res</pre>
## 答案
```
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
;
}
};
```
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录