zoukankan      html  css  js  c++  java
  • 2018秋招小红书算法方向在线编程题

    代码如下:

    class TreeNode:
        def __init__(self, x):
            self.left=None
            self.right=None
            self.value=x
    
    def BuildTree(ceng, zhong):
        if len(ceng)==0:
            return None
        if len(ceng)==1:
            return TreeNode(ceng[0])
        else:
            flag=TreeNode(ceng[0])
            root=ceng[0]
            zong=zhong[:zhong.index(ceng[0])]
            cen=[]
            for i in ceng:
                if i in zong:
                    cen.append(i)
            flag.left=BuildTree(cen,zong)
            cen = []
            zong=zhong[zhong.index(ceng[0])+1:]
            for i in ceng:
                if i in zong:
                    cen.append(i)
            flag.right=BuildTree(cen,zong)
            return flag
    
    
    def PrintLeafNode(root,node):
        if root==None:
            return
        if root.left==None and root.right==None:
            node.append(root.value)
        PrintLeafNode(root.left, node)
        PrintLeafNode(root.right, node)
    
    
    def PreTravel(root,node):
        if root==None:
            return
        node.append(root.value)
        PreTravel(root.left, node)
        PreTravel(root.right, node)
    
    
    def BackTravel(root, node):
        if root==None:
            return
        BackTravel(root.left, node)
        BackTravel(root.right, node)
        node.append(root.value)
    
    
    
    ceng=input().strip().split(" ")
    zhong=input().strip().split(" ")
    root=BuildTree(ceng, zhong)
    leaf=[]
    pre=[]
    back=[]
    PrintLeafNode(root, leaf)
    PreTravel(root, pre)
    BackTravel(root, back)
    print(" ".join(leaf))
    print(" ".join(pre))
    print(" ".join(back))

     第二题:

    代码如下:

    def ac(list1):
        if len(list1)==1:
            return 1
        count = 0
        for i in range(len(list1)):
    
            if i == 0:
                pre = 0
                back = int("".join(list1[i + 1:])) + 1
            elif i == len(list1) - 1:
                pre = int("".join(list1[:i]))
                back =1
            else:
                pre = int("".join(list1[:i]))
                back = int("".join(list1[i + 1:])) + 1
            if list1[i] == '0':
                length = len(list1) - i - 1
                tmp = (pre * (10 ** length))
                count += tmp
            elif list1[i] == '1':
                length = len(list1) - i - 1
                tmp = (pre * (10 ** length))
                count += tmp
                count += (back)
            else:
                length = len(list1) - i - 1
                k = pre + 1
                tmp = k * (10 ** length)
                count += tmp
        return count
    
    list1=list(input().strip())
    print(ac(list1))
  • 相关阅读:
    C#学习之自定义类实现foreach
    C#学习之用迭代器实现枚举器
    silverlight学习之获取照片的路径
    Java语言基础基本数据类型与运算符
    Java语言基础数组
    Asp.Net 学习资源列表
    测试第一篇BLOG文,高亮代码
    当屌丝想看《蜀山剑侠传》[python屏幕抓取]
    jQuery数据显示插件整合
    腾讯面试题:50个阶梯,你一次可以上一阶或两阶,走上去,共有多少种走法【原】
  • 原文地址:https://www.cnblogs.com/tsdblogs/p/9672129.html
Copyright © 2011-2022 走看看