您现在的位置是:首页 > 技术教程 正文

华为OD机试 - 找数字(Java & JS & Python & C & C++)

admin 阅读: 2024-03-24
后台-插件-广告管理-内容页头部广告(手机)
  • 华为OD机试 - 查找树中元素(Java & JS & Python)

    лджшбью坤二爷: dfs想了很久想不出来 个人认为bfs处理这道题更加简单 class DiyTree(): def __init__(self, value: int, clist: list): self.value = value self.clist = clist def tostring(self): return "{0} {1}".format(self.value, self.clist) def bfs(diytreemap: dict, restreemap: dict): v = diytreemap[0].value cl = diytreemap[0].clist restreemap[(0, 0)] = "{" + str(v) + "}" queue = [] level = 1 queue.append((level, cl)) while queue: level, cl = queue.pop(0) nextcl = [] for i, c in enumerate(cl): v = diytreemap[c].value restreemap[(level, i)] = "{" + str(v) + "}" clist = diytreemap[c].clist nextcl.extend(clist) if nextcl: queue.append((level + 1, nextcl))

  • 标签:
    声明

    1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

    在线投稿:投稿 站长QQ:1888636

    后台-插件-广告管理-内容页尾部广告(手机)
    关注我们

    扫一扫关注我们,了解最新精彩内容

    搜索