forked from yingl/LintCodeInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflatten_list.py
More file actions
23 lines (19 loc) · 723 Bytes
/
flatten_list.py
File metadata and controls
23 lines (19 loc) · 723 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# coding: utf-8
class Solution(object):
# @param nestedList a list, each element in the list
# can be a list or integer, for example [1,2,[1,2]]
# @return {int[]} a list of integer
def flatten(self, nestedList):
# Write your code here
if not isinstance(nestedList, list):
return [nestedList]
return self._flatten([], nestedList)
def _flatten(self, _list, nestedList):
if isinstance(nestedList, list):
for i in nestedList:
if isinstance(i, list):
self._flatten(_list, i)
else:
_list.append(i)
return _list
# easy: http://lintcode.com/problem/flatten-list