
本文共 3416 字,大约阅读时间需要 11 分钟。
������������������������������������������������������������������
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
������������
������������������������flatten
���������������������������������������������������������������������������������������������������������������������
result
���������������������s
���result
���������������������������������������s
���������������������������������������������������it
���������������������������������������������������������������try...except
������StopIteration
������������������������������������result
������������������������������������������s
���������������������it
������������������������������������s
���������������������������������������it
������������������������������������
������������������������������������������������������������������������������������������������������������������������������������������������������������������
it
���������try...except
���������������������������������������������������������������������������try...except
������������������������������������������������������������������������������������������������������������������������������������������������������������������
������������
from collections import Iteratorclass Solution(object): def flatten(self, nestedList): if isinstance(nestedList, int): return [nestedList] result = [] stack = [] it = Iterator(nestedList) try: while True: try: item = next(it) if isinstance(item, list): stack.append(it) it = Iterator(item) elif isinstance(item, int): result.append(item) except StopIteration: if stack: it = next(iter(stack.pop())) else: break except StopIteration: pass return result
������������������
������������������������������������������������������������
collections.Iterator
���������������������������������������������������������������������������StopIteration
������������������������stack
���������������������������������������������������StopIteration
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
������������������������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
关于作者
