FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithms/array/flatten.py at master · charlesuko/algorithms · GitHub
charlesuko
/
algorithms
Public
forked from
keon/algorithms
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithms
/
array
/
flatten.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
56 lines (47 loc) · 1.16 KB
Breadcrumbs
algorithms
/
array
/
flatten.py
Copy path
File metadata and controls
56 lines (47 loc) · 1.16 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""
Implement Flatten Arrays.
Given an array that may contain nested arrays,
give a single resultant array.
function flatten(input){
}
Example:
Input: var input = [2, 1, [3, [4, 5], 6], 7, [8]];
flatten(input);
Output: [2, 1, 3, 4, 5, 6, 7, 8]
"""
def
list_flatten
(
l
,
a
=
None
):
a
=
list
(
a
)
if
isinstance
(
a
, (
list
,
tuple
))
else
[]
for
i
in
l
:
if
isinstance
(
i
, (
list
,
tuple
)):
a
=
list_flatten
(
i
,
a
)
else
:
a
.
append
(
i
)
return
a
# stack version
# public static List<Integer> flatten(List<NestedList> l) {
# List<Integer> main = new ArrayList<Integer>();
# Stack<List<NestedList>> stack = new Stack<List<NestedList>>();
# Stack<Integer> indexes = new Stack<Integer>();
# stack.add(l);
# indexes.add(0);
# while (true) {
# if (stack.isEmpty())
# break;
# int index1 = indexes.pop();
# l = stack.pop();
# for (int i = index1; i < l.size(); i++) {
# NestedList n = l.get(i);
# if (n.isInteger()) {
# main.add(n.value);
# } else {
# stack.add(l);
# indexes.add(i+1);
# l = n.list;
# stack.add(l);
# indexes.add(0);
# break;
# }
# }
# }
# return main;
# }
Back
|
FazBrowse Home
|
New Git URL