You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
|[Leetcode-211](https://leetcode.com/problems/design-add-and-search-words-data-structure/)| Design Add And Search Words Data Structure |[c++](./leetcode/211.design-add-and-search-words-data-structure.cpp), [python3](./leetcode/211.design-add-and-search-words-data-structure.py)| Design | O\(26^k \* N\)| O\(LN\)| - |
177
177
|[Leetcode-295](https://leetcode.com/problems/find-median-from-data-stream/)| Find Median From Data Stream |[c++](./leetcode/295.find-median-from-data-stream.cpp), [python3](./leetcode/295.find-median-from-data-stream.py)| Design | O\(logN\)| O\(N\)| - |
|[Leetcode-114](https://leetcode.com/problems/flatten-binary-tree-to-linked-list/)| Flatten Binary Tree To Linked List |[c++](./leetcode/114.flatten-binary-tree-to-linked-list.cpp), [python3](./leetcode/114.flatten-binary-tree-to-linked-list.py)| Stack | O\(N\)| O\(H\)| - |
|[Leetcode-637](https://leetcode.com/problems/average-of-levels-in-binary-tree/)| Average Of Levels In Binary Tree |[c++](./leetcode/637.average-of-levels-in-binary-tree.cpp), [python3](./leetcode/637.average-of-levels-in-binary-tree.py)| Depth-First Search | O\(N\)| O\(N\)| - |
|[Leetcode-543](https://leetcode.com/problems/diameter-of-binary-tree/)| Diameter Of Binary Tree |[c++](./leetcode/543.diameter-of-binary-tree.cpp), [python3](./leetcode/543.diameter-of-binary-tree.py)| Depth-First Search | O\(N\)| O\(H\)| - |
656
660
|[Leetcode-513](https://leetcode.com/problems/find-bottom-left-tree-value/)| Find Bottom Left Tree Value |[c++](./leetcode/513.find-bottom-left-tree-value.cpp), [python3](./leetcode/513.find-bottom-left-tree-value.py)| Depth-First Search | O\(N\)| O\(W\)| - |
657
661
|[Leetcode-114](https://leetcode.com/problems/flatten-binary-tree-to-linked-list/)| Flatten Binary Tree To Linked List |[c++](./leetcode/114.flatten-binary-tree-to-linked-list.cpp), [python3](./leetcode/114.flatten-binary-tree-to-linked-list.py)| Depth-First Search | O\(N\)| O\(H\)| - |
|[Leetcode-41](https://leetcode.com/problems/first-missing-positive/)| First Missing Positive |[python3](./leetcode/41.first-missing-positive.py)| Other |\-|\-| - |
863
868
|[Leetcode-387](https://leetcode.com/problems/first-unique-character-in-a-string/)| First Unique Character In A String |[c++](./leetcode/387.first-unique-character-in-a-string.cpp), [python3](./leetcode/387.first-unique-character-in-a-string.py)| Other |\-|\-| - |
864
869
|[Leetcode-251](https://leetcode.com/problems/flatten-2d-vector/)| Flatten 2D Vector |[python3](./leetcode/251.flatten-2d-vector.py)| Other |\-|\-| - |
865
-
|[Leetcode-341](https://leetcode.com/problems/flatten-nested-list-iterator/)| Flatten Nested List Iterator |[python3](./leetcode/341.flatten-nested-list-iterator.py)| Other |\-|\-| - |
866
870
|[Leetcode-832](https://leetcode.com/problems/flipping-an-image/)| Flipping An Image |[python3](./leetcode/832.flipping-an-image.py)| Other |\-|\-| - |
867
871
|[Leetcode-289](https://leetcode.com/problems/game-of-life/)| Game Of Life |[python3](./leetcode/289.game-of-life.py)| Other |\-|\-| - |
868
872
|[Leetcode-249](https://leetcode.com/problems/group-shifted-strings/)| Group Shifted Strings |[c++](./leetcode/249.group-shifted-strings.cpp)| Other |\-|\-| - |
// You are given a nested list of integers nestedList. Each element is either an integer or a list whose elements may also be integers or other lists. Implement an iterator to flatten it.
8
+
// Implement the NestedIterator class:
9
+
//
10
+
// NestedIterator(List<NestedInteger> nestedList) Initializes the iterator with the nested list nestedList.
11
+
// int next() Returns the next integer in the nested list.
12
+
// boolean hasNext() Returns true if there are still some integers in the nested list and false otherwise.
13
+
//
14
+
// Your code will be tested with the following pseudocode:
15
+
//
16
+
// initialize iterator with nestedList
17
+
// res = []
18
+
// while iterator.hasNext()
19
+
// append iterator.next() to the end of res
20
+
// return res
21
+
//
22
+
// If res matches the expected flattened list, then your code will be judged as correct.
23
+
//
24
+
// Example 1:
25
+
//
26
+
// Input: nestedList = [[1,1],2,[1,1]]
27
+
// Output: [1,1,2,1,1]
28
+
// Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1].
29
+
//
30
+
// Example 2:
31
+
//
32
+
// Input: nestedList = [1,[4,[6]]]
33
+
// Output: [1,4,6]
34
+
// Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6].
35
+
//
36
+
//
37
+
// Constraints:
38
+
//
39
+
// 1 <= nestedList.length <= 500
40
+
// The values of the integers in the nested list is in the range [-106, 106].
41
+
//
42
+
//
43
+
44
+
/**
45
+
* // This is the interface that allows for creating nested lists.
46
+
* // You should not implement it, or speculate about its implementation
47
+
* class NestedInteger {
48
+
* public:
49
+
* // Return true if this NestedInteger holds a single integer, rather than a nested list.
50
+
* bool isInteger() const;
51
+
*
52
+
* // Return the single integer that this NestedInteger holds, if it holds a single integer
53
+
* // The result is undefined if this NestedInteger holds a nested list
54
+
* int getInteger() const;
55
+
*
56
+
* // Return the nested list that this NestedInteger holds, if it holds a nested list
57
+
* // The result is undefined if this NestedInteger holds a single integer
# You are given a nested list of integers nestedList. Each element is either an integer or a list whose elements may also be integers or other lists. Implement an iterator to flatten it.
8
+
# Implement the NestedIterator class:
9
+
#
10
+
# NestedIterator(List<NestedInteger> nestedList) Initializes the iterator with the nested list nestedList.
11
+
# int next() Returns the next integer in the nested list.
12
+
# boolean hasNext() Returns true if there are still some integers in the nested list and false otherwise.
13
+
#
14
+
# Your code will be tested with the following pseudocode:
15
+
#
16
+
# initialize iterator with nestedList
17
+
# res = []
18
+
# while iterator.hasNext()
19
+
# append iterator.next() to the end of res
20
+
# return res
21
+
#
22
+
# If res matches the expected flattened list, then your code will be judged as correct.
23
+
#
24
+
# Example 1:
25
+
#
26
+
# Input: nestedList = [[1,1],2,[1,1]]
27
+
# Output: [1,1,2,1,1]
28
+
# Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1].
29
+
#
30
+
# Example 2:
31
+
#
32
+
# Input: nestedList = [1,[4,[6]]]
33
+
# Output: [1,4,6]
34
+
# Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6].
35
+
#
36
+
#
37
+
# Constraints:
38
+
#
39
+
# 1 <= nestedList.length <= 500
40
+
# The values of the integers in the nested list is in the range [-106, 106].
41
+
#
42
+
#
43
+
1
44
# """
2
45
# This is the interface that allows for creating nested lists.
3
46
# You should not implement it, or speculate about its implementation
4
47
# """
5
-
#class NestedInteger(object):
6
-
# def isInteger(self):
48
+
#class NestedInteger:
49
+
# def isInteger(self) -> bool:
7
50
# """
8
51
# @return True if this NestedInteger holds a single integer, rather than a nested list.
9
-
# :rtype bool
10
52
# """
11
53
#
12
-
# def getInteger(self):
54
+
# def getInteger(self) -> int:
13
55
# """
14
56
# @return the single integer that this NestedInteger holds, if it holds a single integer
15
57
# Return None if this NestedInteger holds a nested list
16
-
# :rtype int
17
58
# """
18
59
#
19
-
# def getList(self):
60
+
# def getList(self) -> [NestedInteger]:
20
61
# """
21
62
# @return the nested list that this NestedInteger holds, if it holds a nested list
22
63
# Return None if this NestedInteger holds a single integer
23
-
# :rtype List[NestedInteger]
24
64
# """
25
65
26
-
classNestedIterator(object):
27
-
28
-
def__init__(self, nestedList):
29
-
"""
30
-
Initialize your data structure here.
31
-
:type nestedList: List[NestedInteger]
32
-
"""
66
+
classNestedIterator:
67
+
def__init__(self, nestedList: [NestedInteger]):
33
68
self.stack=nestedList[::-1]
34
-
35
-
defnext(self):
36
-
"""
37
-
:rtype: int
38
-
"""
69
+
70
+
defnext(self) ->int:
39
71
returnself.stack.pop().getInteger()
40
72
41
-
defhasNext(self):
42
-
"""
43
-
:rtype: bool
44
-
"""
45
-
whileself.stack:
46
-
top=self.stack[-1]
47
-
iftop.isInteger():
73
+
defhasNext(self) ->bool:
74
+
whilelen(self.stack) >0:
75
+
cur=self.stack[-1]
76
+
ifcur.isInteger():
48
77
returnTrue
49
78
else:
50
-
self.stack=self.stack[:-1] +top.getList()[::-1]
51
-
79
+
self.stack.pop()
80
+
self.stack.extend(reversed(cur.getList()))
81
+
52
82
returnFalse
53
-
54
83
55
84
# Your NestedIterator object will be instantiated and called as such:
0 commit comments