快手工程类b卷前两题ac代码,第三题没写出来求大佬教育

Python解法

1.奖金

# -*- coding=utf-8 -*-
class Solution():
    def get_most_money(self, n, nums):
        if n < 2:
            return 0
        low, high, max_money = 0, n-1, 0
        sum_low, sum_high = nums[low], nums[high]
        while low < high:
            if sum_low == sum_high:
                max_money = max(sum_low, max_money)
                low += 1
                high -= 1
                sum_low += nums[low]
                sum_high += nums[high]
            elif sum_low < sum_high:
                low += 1
                sum_low += nums[low]
            else:
                high -= 1
                sum_high += nums[high]

        return max_money


if __name__ == "__main__":
    n = int(input())
    nums = list(map(int, input().split()))
    ex = Solution()
    print(ex.get_most_money(n, nums))

2. 求和树

# -*- coding=utf-8 -*-
class TreeNode():
    def __init__(self, val):
        self.val = val
        self.left = None
        self.right = None


class Solution():
    def recreate_tree(self, pre_order, mid_order):
        if len(pre_order) != len(mid_order):
            return None
        if not pre_order or not mid_order:
            return None

        root_val = pre_order[0]
        root = TreeNode(root_val)
        root_index = mid_order.index(root_val)
        left_pre = pre_order[1:root_index+1]
        left_mid = mid_order[:root_index]
        right_pre = pre_order[root_index+1:]
        right_mid = mid_order[root_index+1:]
        root.left = self.recreate_tree(left_pre, left_mid)
        root.right = self.recreate_tree(right_pre, right_mid)

        return root

    def sum_tree(self, root):
        if not root.left and not root.right:
            temp = root.val
            # 将自己设置成左右子树的和
            root.val = 0
            return temp

        res = 0
        if root.left:
            res += self.sum_tree(root.left)
        if root.right:
            res += self.sum_tree(root.right)

        temp = root.val
        root.val = res
        res += temp
        return res

    def morris_tranverse(self, root):
        if root is None:
            return None
        cur = root
        while cur:
            most_right = cur.left
            if most_right:
                while most_right.right and most_right.right != cur:
                    most_right = most_right.right

                if most_right.right is None:
                    # 第一次访问到cur节点
                    most_right.right = cur
                    cur = cur.left

                elif most_right.right == cur:
                    # 第二次来到cur节点
                    print(cur.val, end=' ')
                    most_right.right = None
                    cur = cur.right
            else:
                print(cur.val, end=' ')
                cur = cur.right
        print('')


if __name__ == "__main__":
    pre_order = list(map(int, input().split()))
    mid_order = list(map(int, input().split()))

    ex = Solution()
    root = ex.recreate_tree(pre_order, mid_order)

    ex.sum_tree(root)
    ex.morris_tranverse(root)

#快手#
全部评论
第三题排序之后就是最长不下降子序列,用经典算法解即可
点赞 回复 分享
发布于 2018-09-25 21:15
题目不一样,我的是字符串后六位转整数,最长回文子序列,latex阅读器那三题
点赞 回复 分享
发布于 2018-09-25 21:13
morris好骚啊
点赞 回复 分享
发布于 2018-09-25 21:31
求问大佬第二题思路是什么
点赞 回复 分享
发布于 2018-09-25 21:37

相关推荐

点赞 评论 收藏
分享
把球:这个听过,你加了就会发现是字节的hr
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务