22. Flatten List
发布日期:2021-05-13 00:13:01 浏览次数:12 分类:精选文章

本文共 3416 字,大约阅读时间需要 11 分钟。

������������������������������������������������������������������

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

������������

������������������������flatten���������������������������������������������������������������������������������������������������������������������

  • ������������������������������������������������������������������������������������������������������������������
  • ������������������������������������������result���������������������s���result���������������������������������������s���������������������������������������������������
  • ������������������it���������������������������������������������������������������try...except������StopIteration������������������������������������
  • ������������������������������������������������������������result������������������������������������������s���������������������it������������������������������������
  • ������������������������������������������������������s���������������������������������������it������������������
  • ������������������

    ������������������������������������������������������������������������������������������������������������������������������������������������������������������

  • ���������������������������������������������������������������������������������������������������������
  • ���������������������������������it���������try...except���������������������������������������������������������������������������
  • ������������������������������������������������������������������������������������try...except���������������������������
  • ���������������������������������������������������������������������������������������������������������������������������������������

    ������������

    from collections import Iterator
    class 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������������������������������������������
  • ���������������������������������������������������������������������������������������������������������������������������������������

    ������������������������������������������������������������������������������������������������������������������������������������

    上一篇:LFU Cache
    下一篇:python多进程 写日志问题

    发表评论

    最新留言

    感谢大佬
    [***.8.128.20]2025年04月15日 19时11分03秒