万卷网 > 题目详情
题型:单选题

小朋友们去邻里拜年,每个家里有不同数量的糖果。规则是:不能连续进入两个相邻的房子(即不能同时取相邻两家的糖果)。目标是拿到最多糖果。以下是代码实现,请补全横线。

def visit(nums):
    if not nums:
        return 0

    size = len(nums)
    if size == 1:
        return nums[0]

    dp = [0] * size
    dp[0] = nums[0]
    dp[1] = max(nums[0], nums[1])

    for i in range(2, size):
        dp[i] = max(dp[i-1], dp[i-2] + nums[i])  # 填空处

    return dp[size - 1]

if __name__ == "__main__":
    nums1 = [1, 2, 3, 1]
    print(visit(nums1))

    nums2 = [2, 7, 9, 3, 1]
    print(visit(nums2))

    nums3 = [5]
    print(visit(nums3))

    nums4 = []
    print(visit(nums4))


A.

dp [i] = dp [i - 1] + nums [i]

B.

dp [i] = max (dp [i - 1], dp [i - 2] * nums [i])

C.

dp [i] = max (dp [i - 1], dp [i - 2] + nums [i])

D.

dp [i] = dp [i - 2] + nums [i]

更新时间:2026-09-17 20:45:15 |
【知识点】 CCF—GESP Python六级

相似题推荐

单选题

给定一棵二叉树,采用广度优先搜索(BFS) 算法,返回右视图所有节点的值。其中右视图定义为:二叉树的右视图是从树的右侧看过去时可见的节点集合,即右视图中的每个节点都是某一层中最右侧的节点。

class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

def rightSideView(root):
    rightmost_value_at_depth = {}
    max_depth = -1

    node_queue = []
    depth_queue = []
    node_queue.append(root)
    depth_queue.append(0)

    while node_queue:
        node = node_queue.pop(0)
        depth = depth_queue.pop(0)

        if node is not None:
            max_depth = max(max_depth, depth)
            rightmost_value_at_depth[depth] = node.val

            node_queue.append(node.left)
            node_queue.append(node.right)

            # (1) 填空处
            depth_queue.append(depth + 1)
            depth_queue.append(depth + 1)

    right_view = []
    # (2) 填空处
    for depth in range(max_depth + 1):
        right_view.append(rightmost_value_at_depth[depth])

    return right_view

if __name__ == "__main__":
    root = TreeNode(1)
    root.left = TreeNode(2)
    root.right = TreeNode(3)
    root.left.right = TreeNode(5)
    root.right.right = TreeNode(4)

    print(rightSideView(root))


A.

(1)depth , depth ;(2)0 , max_depth

B.

(1)depth + 1 , depth + 1 ;(2)0 , max_depth + 1

C.

(1)depth + 1 , depth + 1 ;(2)1 , max_depth + 1

D.

(1)depth , depth ;(2)1 , max_depth

2026-09-18
单选题

游乐园的过山车每次限坐4 人,用循环队列管理排队(容量MAX=5,空一格判满)。下面代码执行后,循环队列是否已满?rear 的值是多少?

MAX = 5
queue = [None] * MAX
front = 0
rear = 0

def enqueue(x):
    global rear, queue
    queue[rear] = x
    rear = (rear + 1) % MAX

def dequeue():
    global front
    front = (front + 1) % MAX

if __name__ == "__main__":
    enqueue(1)
    enqueue(2)
    enqueue(3)
    enqueue(4)

    dequeue()
    dequeue()

    enqueue(5)
    enqueue(6)

    print("队列数组: ", queue)
    print("front下标位置: ", front)
    print("rear下标位置: ", rear)


A.

已满,rear = 1

B.

未满,rear = 1

C.

已满,rear = 2

D.

未满,rear = 4

2026-09-18
判断题

设二叉树共有n个结点,函数 preorderTraversal 的时间复杂度为O(n),空间复杂度为O(n)。

class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

def preorder(root, res):
    if root is None:
        return
    res.append(root.val)
    preorder(root.left, res)
    preorder(root.right, res)

def preorderTraversal(root):
    res = []
    preorder(root, res)
    return res

if __name__ == "__main__":
    root = TreeNode(1)
    root.right = TreeNode(2)
    root.right.left = TreeNode(3)
    result = preorderTraversal(root)
    print("前序遍历结果: ", result)
A.正确 B.错误
2026-09-18
单选题

在二叉搜索树(BST)中,若中序遍历的序列为{1, 2, 3, 4, 5},且先序遍历的第一个序列元素为3,则下列说法正确的是()。

A.

该树一定是一棵完全二叉树。

B.

元素4和5不可能是兄弟节点。

C.

元素1所在节点的深度可能大于3(根节点深度为1)。

D.

元素2一定是元素1的父节点。

2026-09-18
判断题

Python 中的继承支持单继承和多继承,但子类无法直接访问父类的私有成员。

A.正确 B.错误
2026-09-18
公众号
客服 反馈
顶部