Skip to content

Commit 1567f48

Browse files
committed
add 22
1 parent 5f4a2f5 commit 1567f48

File tree

4 files changed

+216
-4
lines changed

4 files changed

+216
-4
lines changed

README.md

Lines changed: 4 additions & 3 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/92 | 17 vips |
48+
| [🔲] | [9c-advanced.md](./list/9c-advanced.md) | 70/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**: 442 problems
52+
**Solved**: 443 problems
5353

5454
## 类型/Category
5555

@@ -155,7 +155,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
155155

156156
## Simulation
157157

158-
| Link | Problem(8) | Solution | Tag | Time | Space | Ref |
158+
| Link | Problem(9) | Solution | Tag | Time | Space | Ref |
159159
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
160160
| [Leetcode-67](https://leetcode.com/problems/add-binary/) | Add Binary | [c++](./leetcode/67.add-binary.cpp), [python3](./leetcode/67.add-binary.py) | Simulation | O\(M\+N\) | O\(1\) | - |
161161
| [Leetcode-415](https://leetcode.com/problems/add-strings/) | Add Strings | [c++](./leetcode/415.add-strings.cpp), [python3](./leetcode/415.add-strings.py) | Simulation | O\(N\) | O\(1\) | - |
@@ -164,6 +164,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
164164
| [Lintcode-849](https://www.lintcode.com/problem/basic-calculator-iii/) | Basic Calculator III | [c++](./lintcode/849.basic-calculator-iii.cpp), [python3](./lintcode/849.basic-calculator-iii.py) | Simulation | O\(N\) | O\(1\) | Leetcode-772 |
165165
| [Lintcode-553](https://www.lintcode.com/problem/bomb-enemy/) | Bomb Enemy | [c++](./lintcode/553.bomb-enemy.cpp), [python3](./lintcode/553.bomb-enemy.py) | Simulation | O\(NM\) | O\(NM\) | Leetcode-361 |
166166
| [Lintcode-366](https://www.lintcode.com/problem/fibonacci/) | Fibonacci | [c++](./lintcode/366.fibonacci.cpp), [python3](./lintcode/366.fibonacci.py) | Simulation | O\(N\) | O\(N\) | - |
167+
| [Lintcode-22](https://www.lintcode.com/problem/flatten-list/) | Flatten List | [c++](./lintcode/22.flatten-list.cpp), [python3](./lintcode/22.flatten-list.py) | Simulation | O\(N\) | O\(N\) | - |
167168
| [Lintcode-39](https://www.lintcode.com/problem/recover-rotated-sorted-array/) | Recover Rotated Sorted Array | [c++](./lintcode/39.recover-rotated-sorted-array.cpp), [python3](./lintcode/39.recover-rotated-sorted-array.py) | Simulation | O\(N\) | O\(1\) | - |
168169

169170
## Design

lintcode/22.flatten-list.cpp

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
// Tag: Simulation
2+
// Time: O(N)
3+
// Space: O(N)
4+
// Ref: -
5+
// Note: -
6+
7+
// Given a list, each element in the list can be a list or an integer.Flatten it into a simply list with integers.
8+
//
9+
// **Example 1:**
10+
//
11+
// Input:
12+
// ```
13+
// list = [[1,1],2,[1,1]]
14+
// ```
15+
// Output:
16+
// ```
17+
// [1,1,2,1,1]
18+
// ```
19+
// Explanation:
20+
//
21+
// Flatten it into a simply list with integers.
22+
//
23+
// **Example 2:**
24+
//
25+
// Input:
26+
// ```
27+
// list = [1,2,[1,2]]
28+
// ```
29+
// Output:
30+
// ```
31+
// [1,2,1,2]
32+
// ```
33+
// Explanation:
34+
//
35+
// Flatten it into a simply list with integers.
36+
//
37+
// **Example 3:**
38+
//
39+
// Input:
40+
// ```
41+
// list = [4,[3,[2,[1]]]]
42+
// ```
43+
// Output:
44+
// ```
45+
// [4,3,2,1]
46+
// ```
47+
// Explanation:
48+
//
49+
// Flatten it into a simply list with integers.
50+
//
51+
// If the element in the given list is a list, it can contain list too.
52+
53+
/**
54+
* // This is the interface that allows for creating nested lists.
55+
* // You should not implement it, or speculate about its implementation
56+
* class NestedInteger {
57+
* public:
58+
* // Return true if this NestedInteger holds a single integer,
59+
* // rather than a nested list.
60+
* bool isInteger() const;
61+
*
62+
* // Return the single integer that this NestedInteger holds,
63+
* // if it holds a single integer
64+
* // The result is undefined if this NestedInteger holds a nested list
65+
* int getInteger() const;
66+
*
67+
* // Return the nested list that this NestedInteger holds,
68+
* // if it holds a nested list
69+
* // The result is undefined if this NestedInteger holds a single integer
70+
* const vector<NestedInteger> &getList() const;
71+
* };
72+
*/
73+
class Solution {
74+
public:
75+
// @param nestedList a list of NestedInteger
76+
// @return a list of integer
77+
vector<int> flatten(vector<NestedInteger> &nestedList) {
78+
// Write your code here
79+
vector<int> res;
80+
flatten(nestedList, res);
81+
return res;
82+
}
83+
84+
void flatten(const vector<NestedInteger> &nestedList, vector<int>& result) {
85+
for (auto x : nestedList) {
86+
if (x.isInteger()) {
87+
result.push_back(x.getInteger());
88+
} else {
89+
flatten(x.getList(), result);
90+
}
91+
}
92+
}
93+
};
94+
95+
class Solution {
96+
public:
97+
// @param nestedList a list of NestedInteger
98+
// @return a list of integer
99+
vector<int> flatten(vector<NestedInteger> &nestedList) {
100+
// Write your code here
101+
vector<int> result;
102+
stack<NestedInteger> stack;
103+
104+
for (int i = nestedList.size() - 1; i >= 0; i--) {
105+
stack.push(nestedList[i]);
106+
}
107+
108+
while (!stack.empty()) {
109+
NestedInteger cur = stack.top();
110+
stack.pop();
111+
112+
if (cur.isInteger()) {
113+
result.push_back(cur.getInteger());
114+
} else {
115+
auto &list = cur.getList();
116+
for (int i = list.size() - 1; i >= 0; i--) {
117+
stack.push(list[i]);
118+
}
119+
}
120+
}
121+
122+
return result;
123+
}
124+
};

lintcode/22.flatten-list.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Tag: Simulation
2+
# Time: O(N)
3+
# Space: O(N)
4+
# Ref: -
5+
# Note: -
6+
7+
# Given a list, each element in the list can be a list or an integer.Flatten it into a simply list with integers.
8+
#
9+
# **Example 1:**
10+
#
11+
# Input:
12+
# ```
13+
# list = [[1,1],2,[1,1]]
14+
# ```
15+
# Output:
16+
# ```
17+
# [1,1,2,1,1]
18+
# ```
19+
# Explanation:
20+
#
21+
# Flatten it into a simply list with integers.
22+
#
23+
# **Example 2:**
24+
#
25+
# Input:
26+
# ```
27+
# list = [1,2,[1,2]]
28+
# ```
29+
# Output:
30+
# ```
31+
# [1,2,1,2]
32+
# ```
33+
# Explanation:
34+
#
35+
# Flatten it into a simply list with integers.
36+
#
37+
# **Example 3:**
38+
#
39+
# Input:
40+
# ```
41+
# list = [4,[3,[2,[1]]]]
42+
# ```
43+
# Output:
44+
# ```
45+
# [4,3,2,1]
46+
# ```
47+
# Explanation:
48+
#
49+
# Flatten it into a simply list with integers.
50+
#
51+
# If the element in the given list is a list, it can contain list too.
52+
53+
from collections import deque
54+
class Solution(object):
55+
56+
# @param nestedList a list, each element in the list
57+
# can be a list or integer, for example [1,2,[1,2]]
58+
# @return {int[]} a list of integer
59+
def flatten(self, nestedList):
60+
# Write your code here
61+
res = []
62+
for x in nestedList:
63+
if isinstance(x, list):
64+
res += self.flatten(x)
65+
else:
66+
res.append(x)
67+
68+
return res
69+
70+
class Solution(object):
71+
# @param nestedList a list, each element in the list
72+
# can be a list or integer, for example [1,2,[1,2]]
73+
# @return {int[]} a list of integer
74+
def flatten(self, nestedList):
75+
# Write your code here
76+
res = []
77+
stack = []
78+
for x in reversed(nestedList):
79+
stack.append(x)
80+
81+
while len(stack) > 0:
82+
cur = stack.pop()
83+
if isinstance(cur, int):
84+
res.append(cur)
85+
else:
86+
stack.extend(reversed(cur))
87+
88+
return res

list/9c-advanced.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@
105105
86. https://www.lintcode.com/problem/wiggle-sort/
106106
87. https://leetcode.com/problems/wiggle-sort-ii/
107107

108-
- https://www.lintcode.com/problem/nuts-bolts-problem/
109108
- https://www.lintcode.com/problem/flatten-list/
110109
- https://www.lintcode.com/problem/flatten-nested-list-iterator/
111110
- https://www.lintcode.com/problem/flatten-2d-vector/

0 commit comments

Comments
 (0)