三个结果都对得上吗

👁️ 0 人浏览 💬 0 人评论 ❤️ 添加收藏

运行下面这段程序:

def two_sum(a, target):
    lo = 0
    hi = len(a) - 1
    while lo < hi:
        s = a[lo] + a[hi]
        if s == target:
            return lo, hi
        if s < target:
            lo += 1
        else:
            hi -= 1
    return -1, -1

def mid_index(a):
    slow = 0
    fast = 0
    while fast + 1 < len(a):
        slow += 1
        fast += 2
    return slow

def max_window(a, k):
    s = 0
    for i in range(k):
        s += a[i]
    best = s
    for i in range(k, len(a)):
        s += a[i] - a[i - k]
        if s > best:
            best = s
    return best

lo, hi = two_sum([13, 15, 17, 23, 24], 40)
ok = (lo == 2 and hi == 3 and mid_index([13, 15, 17, 23, 24]) == 2
      and max_window([17, 8, 15, 13, 23, 24, 19], 3) == 66)
print(ok)
提交你的答案
请登录后提交答案。
去登录
代码编辑器
Ctrl + Enter 运行
本次输入:
输出:

                        
👩‍🏫
AI
💬 题目评论

全部评论