Skip to content

Commit 5078ba8

Browse files
committed
feat: add cpp solution to lc problem: No.0341
No.0341.Flatten Nested List Iterator
1 parent 6e9dcd4 commit 5078ba8

File tree

4 files changed

+169
-4
lines changed

4 files changed

+169
-4
lines changed

lcci/17.09.Get Kth Magic Number/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@
2525

2626
**方法二:动态规划**
2727

28-
定义数组 $dp$,其中 $dp[i]$ 表示第 $i$ 个数,答案即为 $dp[k]$。
28+
方法一的做法足以通过本题,但如果在面试中,面试官可能要求我们实现一个复杂度更低的算法。因此,我们有必要掌握一种更优的算法。
29+
30+
我们定义数组 $dp$,其中 $dp[i]$ 表示第 $i$ 个数,答案即为 $dp[k]$。
2931

3032
定义三个指针 $p_3$, $p_5$, $p_7$,表示下一个数是当前指针指向的数乘以对应的质因数,初始值都为 $1$。
3133

32-
当 $2\le i \le k$ 时,令 $dp[i] = \min(dp[p_3\times 3], dp[p_5]\times 5, dp[p_7]\times 7)$,然后分别比较 $dp[i]$ 和 $dp[p_3]\times 3, dp[p_5]\times 5, dp[p_7]\times 7$,如果相等,则将对应的指针加 $1$。
34+
当 $2\le i \le k$ 时,令 $dp[i] = \min(dp[p_3]\times 3, dp[p_5]\times 5, dp[p_7]\times 7)$,然后分别比较 $dp[i]$ 和 $dp[p_3]\times 3, dp[p_5]\times 5, dp[p_7]\times 7$,如果相等,则将对应的指针加 $1$。
3335

3436
时间复杂度 $O(k)$,空间复杂度 $O(k)$。
3537

solution/0300-0399/0341.Flatten Nested List Iterator/README.md

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ return res</pre>
5959

6060
**方法一:递归**
6161

62-
根据题意要求可以将 NestedInteger 数据结构视作一个 N 叉树,当为一个整数时该节点是 N 叉树的叶子节点,当为一个整数数组时该节点是 N 叉树的非叶子节点,数组中的每一个元素包含子树的所有节点。故直接递归遍历 N 叉树并记录所有的叶子节点即可。
62+
根据题意要求可以将 NestedInteger 数据结构视作一个 N 叉树,当元素为一个整数时,该节点是 N 叉树的叶子节点,当元素为一个整数数组时,该节点是 N 叉树的非叶子节点,数组中的每一个元素包含子树的所有节点。故直接递归遍历 N 叉树并记录所有的叶子节点即可。
6363

6464
**方法二:直接展开**
6565

66-
调用 hasNext 时,如果 nestedList 的第一个元素是列表类型,则不断展开这个元素,直到第一个元素是整数类型。 调用 Next 方法时,hasNext 方法已确保 nestedList 第一个元素为整数类型,直接返回即可。
66+
调用 hasNext 时,如果 nestedList 的第一个元素是列表类型,则不断展开这个元素,直到第一个元素是整数类型。 调用 Next 方法时,由于 `hasNext()` 方法已确保 nestedList 第一个元素为整数类型,直接返回即可。
6767

6868
<!-- tabs:start -->
6969

@@ -184,6 +184,62 @@ public class NestedIterator implements Iterator<Integer> {
184184
*/
185185
```
186186

187+
### **C++**
188+
189+
```cpp
190+
/**
191+
* // This is the interface that allows for creating nested lists.
192+
* // You should not implement it, or speculate about its implementation
193+
* class NestedInteger {
194+
* public:
195+
* // Return true if this NestedInteger holds a single integer, rather than a nested list.
196+
* bool isInteger() const;
197+
*
198+
* // Return the single integer that this NestedInteger holds, if it holds a single integer
199+
* // The result is undefined if this NestedInteger holds a nested list
200+
* int getInteger() const;
201+
*
202+
* // Return the nested list that this NestedInteger holds, if it holds a nested list
203+
* // The result is undefined if this NestedInteger holds a single integer
204+
* const vector<NestedInteger> &getList() const;
205+
* };
206+
*/
207+
208+
class NestedIterator {
209+
public:
210+
NestedIterator(vector<NestedInteger> &nestedList) {
211+
dfs(nestedList);
212+
}
213+
214+
int next() {
215+
return vals[cur++];
216+
}
217+
218+
bool hasNext() {
219+
return cur < vals.size();
220+
}
221+
private:
222+
vector<int> vals;
223+
int cur = 0;
224+
225+
void dfs(vector<NestedInteger> &nestedList) {
226+
for (auto& e : nestedList) {
227+
if (e.isInteger()) {
228+
vals.push_back(e.getInteger());
229+
} else {
230+
dfs(e.getList());
231+
}
232+
}
233+
}
234+
};
235+
236+
/**
237+
* Your NestedIterator object will be instantiated and called as such:
238+
* NestedIterator i(nestedList);
239+
* while (i.hasNext()) cout << i.next();
240+
*/
241+
```
242+
187243
### **TypeScript**
188244

189245
```ts

solution/0300-0399/0341.Flatten Nested List Iterator/README_EN.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,62 @@ public class NestedIterator implements Iterator<Integer> {
168168
*/
169169
```
170170

171+
### **C++**
172+
173+
```cpp
174+
/**
175+
* // This is the interface that allows for creating nested lists.
176+
* // You should not implement it, or speculate about its implementation
177+
* class NestedInteger {
178+
* public:
179+
* // Return true if this NestedInteger holds a single integer, rather than a nested list.
180+
* bool isInteger() const;
181+
*
182+
* // Return the single integer that this NestedInteger holds, if it holds a single integer
183+
* // The result is undefined if this NestedInteger holds a nested list
184+
* int getInteger() const;
185+
*
186+
* // Return the nested list that this NestedInteger holds, if it holds a nested list
187+
* // The result is undefined if this NestedInteger holds a single integer
188+
* const vector<NestedInteger> &getList() const;
189+
* };
190+
*/
191+
192+
class NestedIterator {
193+
public:
194+
NestedIterator(vector<NestedInteger> &nestedList) {
195+
dfs(nestedList);
196+
}
197+
198+
int next() {
199+
return vals[cur++];
200+
}
201+
202+
bool hasNext() {
203+
return cur < vals.size();
204+
}
205+
private:
206+
vector<int> vals;
207+
int cur = 0;
208+
209+
void dfs(vector<NestedInteger> &nestedList) {
210+
for (auto& e : nestedList) {
211+
if (e.isInteger()) {
212+
vals.push_back(e.getInteger());
213+
} else {
214+
dfs(e.getList());
215+
}
216+
}
217+
}
218+
};
219+
220+
/**
221+
* Your NestedIterator object will be instantiated and called as such:
222+
* NestedIterator i(nestedList);
223+
* while (i.hasNext()) cout << i.next();
224+
*/
225+
```
226+
171227
### **TypeScript**
172228

173229
```ts
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* // This is the interface that allows for creating nested lists.
3+
* // You should not implement it, or speculate about its implementation
4+
* class NestedInteger {
5+
* public:
6+
* // Return true if this NestedInteger holds a single integer, rather than a nested list.
7+
* bool isInteger() const;
8+
*
9+
* // Return the single integer that this NestedInteger holds, if it holds a single integer
10+
* // The result is undefined if this NestedInteger holds a nested list
11+
* int getInteger() const;
12+
*
13+
* // Return the nested list that this NestedInteger holds, if it holds a nested list
14+
* // The result is undefined if this NestedInteger holds a single integer
15+
* const vector<NestedInteger> &getList() const;
16+
* };
17+
*/
18+
19+
class NestedIterator {
20+
public:
21+
NestedIterator(vector<NestedInteger> &nestedList) {
22+
dfs(nestedList);
23+
}
24+
25+
int next() {
26+
return vals[cur++];
27+
}
28+
29+
bool hasNext() {
30+
return cur < vals.size();
31+
}
32+
private:
33+
vector<int> vals;
34+
int cur = 0;
35+
36+
void dfs(vector<NestedInteger> &nestedList) {
37+
for (auto& e : nestedList) {
38+
if (e.isInteger()) {
39+
vals.push_back(e.getInteger());
40+
} else {
41+
dfs(e.getList());
42+
}
43+
}
44+
}
45+
};
46+
47+
/**
48+
* Your NestedIterator object will be instantiated and called as such:
49+
* NestedIterator i(nestedList);
50+
* while (i.hasNext()) cout << i.next();
51+
*/

0 commit comments

Comments
 (0)