diff options
author | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
---|---|---|
committer | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
commit | d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch) | |
tree | 96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /flatten_nested_iterator.cpp | |
download | leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip |
initial
Diffstat (limited to 'flatten_nested_iterator.cpp')
-rw-r--r-- | flatten_nested_iterator.cpp | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/flatten_nested_iterator.cpp b/flatten_nested_iterator.cpp new file mode 100644 index 0000000..4a6f93c --- /dev/null +++ b/flatten_nested_iterator.cpp @@ -0,0 +1,33 @@ +#include "leetcode.h" + +class NestedIterator { +public: + NestedIterator(vector<NestedInteger> &nestedList) { + int n = nestedList.size(); + for (int i = n - 1; i >= 0; --i) + nodes.push(nestedList[i]); + } + int next() { + int ans = nodes.top().getInteger(); + nodes.pop(); + return ans; + } + bool hasNext() { + while (!nodes.empty()) { + NestedInteger curr = nodes.top(); + if (curr.isInteger()) + return true; + nodes.pop(); + vector<NestedInteger> &adj = curr.getList(); + int n = adj.size(); + for (int i = n - 1; i >= 0; --i) + nodes.push(adj[i]); + } + return false; + } + +private: + stack<NestedInteger> nodes; +}; + +int main() {} |