Skip to content

Commit cdfdf55

Browse files
committed
update 601, 341
1 parent 1567f48 commit cdfdf55

8 files changed

+283
-50
lines changed

README.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
4545
| [] | [9c-dp.md](./list/9c-dp.md) | 42/45 | 3 vips |
4646
| [] | [geekbang.md](./list/geekbang.md) | 55/55 | - |
4747
| [] | [leetcode101.md](./list/leetcode101.md) | 183/184 | 1 vip |
48-
| [🔲] | [9c-advanced.md](./list/9c-advanced.md) | 70/91 | 17 vips |
48+
| [🔲] | [9c-advanced.md](./list/9c-advanced.md) | 73/91 | 17 vips |
4949
| [🔲] | [leetcode-contest.md](./list/leetcode-contest.md) | 10/40 | - |
5050
| [🔲] | [leetcode-google.md](./list/leetcode-google.md) | 0/7 | - |
5151

52-
**Solved**: 443 problems
52+
**Solved**: 444 problems
5353

5454
## 类型/Category
5555

@@ -169,12 +169,13 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
169169

170170
## Design
171171

172-
| Link | Problem(15) | Solution | Tag | Time | Space | Ref |
172+
| Link | Problem(17) | Solution | Tag | Time | Space | Ref |
173173
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
174174
| [Leetcode-432](https://leetcode.com/problems/all-oone-data-structure/) | All Oone Data Structure | [c++](./leetcode/432.all-oone-data-structure.cpp), [python3](./leetcode/432.all-oone-data-structure.py) | Design | O\(1\) | O\(N\) | - |
175175
| [Leetcode-173](https://leetcode.com/problems/binary-search-tree-iterator/) | Binary Search Tree Iterator | [c++](./leetcode/173.binary-search-tree-iterator.cpp), [python3](./leetcode/173.binary-search-tree-iterator.py) | Design | O\(1\) | O\(H\) | - |
176176
| [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\) | - |
177177
| [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\) | - |
178+
| [Leetcode-341](https://leetcode.com/problems/flatten-nested-list-iterator/) | Flatten Nested List Iterator | [c++](./leetcode/341.flatten-nested-list-iterator.cpp), [python3](./leetcode/341.flatten-nested-list-iterator.py) | Design | O\(\*1\) | O\(N\) | - |
178179
| [Leetcode-232](https://leetcode.com/problems/implement-queue-using-stacks/) | Implement Queue Using Stacks | [c++](./leetcode/232.implement-queue-using-stacks.cpp), [python3](./leetcode/232.implement-queue-using-stacks.py) | Design | O\(\*1\) | O\(N\) | - |
179180
| [Leetcode-225](https://leetcode.com/problems/implement-stack-using-queues/) | Implement Stack Using Queues | [c++](./leetcode/225.implement-stack-using-queues.cpp), [python3](./leetcode/225.implement-stack-using-queues.py) | Design | O\(N\) | O\(N\) | - |
180181
| [Leetcode-208](https://leetcode.com/problems/implement-trie-prefix-tree/) | Implement Trie Prefix Tree | [c++](./leetcode/208.implement-trie-prefix-tree.cpp), [python3](./leetcode/208.implement-trie-prefix-tree.py) | Design | O\(N\) | O\(L\) | - |
@@ -186,6 +187,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
186187
| [Leetcode-303](https://leetcode.com/problems/range-sum-query-immutable/) | Range Sum Query Immutable | [c++](./leetcode/303.range-sum-query-immutable.cpp), [python3](./leetcode/303.range-sum-query-immutable.py) | Design | O\(\*1\) | O\(N\) | - |
187188
| [Leetcode-307](https://leetcode.com/problems/range-sum-query-mutable/) | Range Sum Query Mutable | [c++](./leetcode/307.range-sum-query-mutable.cpp), [python3](./leetcode/307.range-sum-query-mutable.py) | Design | O\(logN\) | O\(N\) | - |
188189
| [Leetcode-297](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/) | Serialize And Deserialize Binary Tree | [c++](./leetcode/297.serialize-and-deserialize-binary-tree.cpp), [python3](./leetcode/297.serialize-and-deserialize-binary-tree.py) | Design | O\(N\) | O\(N\) | - |
190+
| [Lintcode-601](https://www.lintcode.com/problem/flatten-2d-vector/) | Flatten 2D Vector | [c++](./lintcode/601.flatten-2d-vector.cpp), [python3](./lintcode/601.flatten-2d-vector.py) | Design | O\(\*1\) | O\(N\) | Leetcode-251 |
189191

190192
## Greedy
191193

@@ -413,7 +415,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
413415

414416
## Stack
415417

416-
| Link | Problem(16) | Solution | Tag | Time | Space | Ref |
418+
| Link | Problem(17) | Solution | Tag | Time | Space | Ref |
417419
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
418420
| [Leetcode-227](https://leetcode.com/problems/basic-calculator-ii/) | Basic Calculator II | [c++](./leetcode/227.basic-calculator-ii.cpp), [python3](./leetcode/227.basic-calculator-ii.py) | Stack | O\(N\) | O\(N\) | - |
419421
| [Leetcode-173](https://leetcode.com/problems/binary-search-tree-iterator/) | Binary Search Tree Iterator | [c++](./leetcode/173.binary-search-tree-iterator.cpp), [python3](./leetcode/173.binary-search-tree-iterator.py) | Stack | O\(1\) | O\(H\) | - |
@@ -422,6 +424,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
422424
| [Leetcode-144](https://leetcode.com/problems/binary-tree-preorder-traversal/) | Binary Tree Preorder Traversal | [c++](./leetcode/144.binary-tree-preorder-traversal.cpp), [python3](./leetcode/144.binary-tree-preorder-traversal.py) | Stack | O\(N\) | O\(H\) | - |
423425
| [Leetcode-394](https://leetcode.com/problems/decode-string/) | Decode String | [c++](./leetcode/394.decode-string.cpp), [python3](./leetcode/394.decode-string.py) | Stack | O\(A\) | O\(A\) | - |
424426
| [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\) | - |
427+
| [Leetcode-341](https://leetcode.com/problems/flatten-nested-list-iterator/) | Flatten Nested List Iterator | [c++](./leetcode/341.flatten-nested-list-iterator.cpp), [python3](./leetcode/341.flatten-nested-list-iterator.py) | Stack | O\(\*1\) | O\(N\) | - |
425428
| [Leetcode-232](https://leetcode.com/problems/implement-queue-using-stacks/) | Implement Queue Using Stacks | [c++](./leetcode/232.implement-queue-using-stacks.cpp), [python3](./leetcode/232.implement-queue-using-stacks.py) | Stack | O\(\*1\) | O\(N\) | - |
426429
| [Leetcode-225](https://leetcode.com/problems/implement-stack-using-queues/) | Implement Stack Using Queues | [c++](./leetcode/225.implement-stack-using-queues.cpp), [python3](./leetcode/225.implement-stack-using-queues.py) | Stack | O\(N\) | O\(N\) | - |
427430
| [Leetcode-897](https://leetcode.com/problems/increasing-order-search-tree/) | Increasing Order Search Tree | [c++](./leetcode/897.increasing-order-search-tree.cpp), [python3](./leetcode/897.increasing-order-search-tree.py) | Stack | O\(N\) | O\(H\) | - |
@@ -445,8 +448,9 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
445448

446449
## Queue
447450

448-
| Link | Problem(2) | Solution | Tag | Time | Space | Ref |
451+
| Link | Problem(3) | Solution | Tag | Time | Space | Ref |
449452
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
453+
| [Leetcode-341](https://leetcode.com/problems/flatten-nested-list-iterator/) | Flatten Nested List Iterator | [c++](./leetcode/341.flatten-nested-list-iterator.cpp), [python3](./leetcode/341.flatten-nested-list-iterator.py) | Queue | O\(\*1\) | O\(N\) | - |
450454
| [Leetcode-232](https://leetcode.com/problems/implement-queue-using-stacks/) | Implement Queue Using Stacks | [c++](./leetcode/232.implement-queue-using-stacks.cpp), [python3](./leetcode/232.implement-queue-using-stacks.py) | Queue | O\(\*1\) | O\(N\) | - |
451455
| [Leetcode-225](https://leetcode.com/problems/implement-stack-using-queues/) | Implement Stack Using Queues | [c++](./leetcode/225.implement-stack-using-queues.cpp), [python3](./leetcode/225.implement-stack-using-queues.py) | Queue | O\(N\) | O\(N\) | - |
452456

@@ -641,7 +645,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
641645

642646
## Depth-First Search
643647

644-
| Link | Problem(41) | Solution | Tag | Time | Space | Ref |
648+
| Link | Problem(42) | Solution | Tag | Time | Space | Ref |
645649
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
646650
| [Leetcode-721](https://leetcode.com/problems/accounts-merge/) | Accounts Merge | [c++](./leetcode/721.accounts-merge.cpp), [python3](./leetcode/721.accounts-merge.py) | Depth-First Search | O\(MlogM\) | O\(N\) | - |
647651
| [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\) | - |
@@ -655,6 +659,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
655659
| [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\) | - |
656660
| [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\) | - |
657661
| [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\) | - |
662+
| [Leetcode-341](https://leetcode.com/problems/flatten-nested-list-iterator/) | Flatten Nested List Iterator | [c++](./leetcode/341.flatten-nested-list-iterator.cpp), [python3](./leetcode/341.flatten-nested-list-iterator.py) | Depth-First Search | O\(\*1\) | O\(N\) | - |
658663
| [Leetcode-897](https://leetcode.com/problems/increasing-order-search-tree/) | Increasing Order Search Tree | [c++](./leetcode/897.increasing-order-search-tree.cpp), [python3](./leetcode/897.increasing-order-search-tree.py) | Depth-First Search | O\(N\) | O\(H\) | - |
659664
| [Leetcode-226](https://leetcode.com/problems/invert-binary-tree/) | Invert Binary Tree | [c++](./leetcode/226.invert-binary-tree.cpp), [python3](./leetcode/226.invert-binary-tree.py) | Depth-First Search | O\(N\) | O\(H\) | - |
660665
| [Leetcode-785](https://leetcode.com/problems/is-graph-bipartite/) | Is Graph Bipartite | [c++](./leetcode/785.is-graph-bipartite.cpp), [python3](./leetcode/785.is-graph-bipartite.py) | Depth-First Search | O\(E\+V\) | O\(W\) | - |
@@ -822,7 +827,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
822827

823828
## Other
824829

825-
| Link | Problem(135) | Solution | Tag | Time | Space | Ref |
830+
| Link | Problem(134) | Solution | Tag | Time | Space | Ref |
826831
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
827832
| [Leetcode-16](https://leetcode.com/problems/3sum-closest/) | 3Sum Closest | [c++](./leetcode/16.3sum-closest.cpp), [python3](./leetcode/16.3sum-closest.py) | Other | \- | \- | - |
828833
| [Leetcode-454](https://leetcode.com/problems/4sum-ii/) | 4Sum II | [c++](./leetcode/454.4sum-ii.cpp) | Other | \- | \- | - |
@@ -862,7 +867,6 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
862867
| [Leetcode-41](https://leetcode.com/problems/first-missing-positive/) | First Missing Positive | [python3](./leetcode/41.first-missing-positive.py) | Other | \- | \- | - |
863868
| [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 | \- | \- | - |
864869
| [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 | \- | \- | - |
866870
| [Leetcode-832](https://leetcode.com/problems/flipping-an-image/) | Flipping An Image | [python3](./leetcode/832.flipping-an-image.py) | Other | \- | \- | - |
867871
| [Leetcode-289](https://leetcode.com/problems/game-of-life/) | Game Of Life | [python3](./leetcode/289.game-of-life.py) | Other | \- | \- | - |
868872
| [Leetcode-249](https://leetcode.com/problems/group-shifted-strings/) | Group Shifted Strings | [c++](./leetcode/249.group-shifted-strings.cpp) | Other | \- | \- | - |
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Tag: Stack, Tree, Depth-First Search, Design, Queue, Iterator
2+
// Time: O(*1)
3+
// Space: O(N)
4+
// Ref: -
5+
// Note: -
6+
7+
// 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
58+
* const vector<NestedInteger> &getList() const;
59+
* };
60+
*/
61+
62+
class NestedIterator {
63+
public:
64+
stack<NestedInteger> st;
65+
NestedIterator(vector<NestedInteger> &nestedList) {
66+
for (int i = nestedList.size() - 1; i >= 0; i--) {
67+
st.push(nestedList[i]);
68+
}
69+
}
70+
71+
int next() {
72+
NestedInteger top = st.top();
73+
st.pop();
74+
return top.getInteger();
75+
}
76+
77+
bool hasNext() {
78+
while (!st.empty()) {
79+
NestedInteger cur = st.top();
80+
if (cur.isInteger()) {
81+
return true;
82+
} else {
83+
st.pop();
84+
auto &list = cur.getList();
85+
for (int i = list.size() - 1; i >= 0; i--){
86+
st.push(list[i]);
87+
}
88+
}
89+
}
90+
return false;
91+
}
92+
};
93+
94+
/**
95+
* Your NestedIterator object will be instantiated and called as such:
96+
* NestedIterator i(nestedList);
97+
* while (i.hasNext()) cout << i.next();
98+
*/

leetcode/341.flatten-nested-list-iterator.py

Lines changed: 58 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,85 @@
1+
# Tag: Stack, Tree, Depth-First Search, Design, Queue, Iterator
2+
# Time: O(*1)
3+
# Space: O(N)
4+
# Ref: -
5+
# Note: -
6+
7+
# 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+
144
# """
245
# This is the interface that allows for creating nested lists.
346
# You should not implement it, or speculate about its implementation
447
# """
5-
#class NestedInteger(object):
6-
# def isInteger(self):
48+
#class NestedInteger:
49+
# def isInteger(self) -> bool:
750
# """
851
# @return True if this NestedInteger holds a single integer, rather than a nested list.
9-
# :rtype bool
1052
# """
1153
#
12-
# def getInteger(self):
54+
# def getInteger(self) -> int:
1355
# """
1456
# @return the single integer that this NestedInteger holds, if it holds a single integer
1557
# Return None if this NestedInteger holds a nested list
16-
# :rtype int
1758
# """
1859
#
19-
# def getList(self):
60+
# def getList(self) -> [NestedInteger]:
2061
# """
2162
# @return the nested list that this NestedInteger holds, if it holds a nested list
2263
# Return None if this NestedInteger holds a single integer
23-
# :rtype List[NestedInteger]
2464
# """
2565

26-
class NestedIterator(object):
27-
28-
def __init__(self, nestedList):
29-
"""
30-
Initialize your data structure here.
31-
:type nestedList: List[NestedInteger]
32-
"""
66+
class NestedIterator:
67+
def __init__(self, nestedList: [NestedInteger]):
3368
self.stack = nestedList[::-1]
34-
35-
def next(self):
36-
"""
37-
:rtype: int
38-
"""
69+
70+
def next(self) -> int:
3971
return self.stack.pop().getInteger()
4072

41-
def hasNext(self):
42-
"""
43-
:rtype: bool
44-
"""
45-
while self.stack:
46-
top = self.stack[-1]
47-
if top.isInteger():
73+
def hasNext(self) -> bool:
74+
while len(self.stack) > 0:
75+
cur = self.stack[-1]
76+
if cur.isInteger():
4877
return True
4978
else:
50-
self.stack = self.stack[:-1] + top.getList()[::-1]
51-
79+
self.stack.pop()
80+
self.stack.extend(reversed(cur.getList()))
81+
5282
return False
53-
5483

5584
# Your NestedIterator object will be instantiated and called as such:
5685
# i, v = NestedIterator(nestedList), []

lintcode/22.flatten-list.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,22 +99,22 @@ class Solution {
9999
vector<int> flatten(vector<NestedInteger> &nestedList) {
100100
// Write your code here
101101
vector<int> result;
102-
stack<NestedInteger> stack;
102+
stack<NestedInteger> st;
103103

104104
for (int i = nestedList.size() - 1; i >= 0; i--) {
105-
stack.push(nestedList[i]);
105+
st.push(nestedList[i]);
106106
}
107107

108-
while (!stack.empty()) {
109-
NestedInteger cur = stack.top();
110-
stack.pop();
108+
while (!st.empty()) {
109+
NestedInteger cur = st.top();
110+
st.pop();
111111

112112
if (cur.isInteger()) {
113113
result.push_back(cur.getInteger());
114114
} else {
115115
auto &list = cur.getList();
116116
for (int i = list.size() - 1; i >= 0; i--) {
117-
stack.push(list[i]);
117+
st.push(list[i]);
118118
}
119119
}
120120
}

0 commit comments

Comments
 (0)