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

采用如下代码实现检查输入的字符串括号是否匹配,横线上应填入的代码为( )。

class Stack:
	def __init__(self):
		self.items = []
	def is_empty(self):
		return not self.items
	def push(self, item):
		self.items.append(item)
	def pop(self):
		if not self.is_empty():
			return self.items.pop()
	def peek(self):
		if not self.is_empty():
			return self.items[-1]
	def size(self):
		return len(self.items)
		
def paren_match(expr):
	s = Stack()
	balanced = True
	index = 0
	while index < len(expr) and balanced:
		symbol = expr[index]
		if symbol in '([{':
			________________
		else:
			if s.is_empty():
				balanced = False
			else:
				top = s.pop()
			if not matches(top, symbol):
				balanced = False
		index += 1
		
	if balanced and s.is_empty():
		return True
	else:
		return False
	
def matches(opening, closing):
	opens = '([{'
	closers = ')]}'
	return opening in opens and closers.index(closing) == opens.index(opening)
A.

s.push(symbol)

B.

s.pop(symbol)

C.

s.push(index)

D.

s.pop(index)

更新时间:2025-02-21 13:45:34 |
【知识点】 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
公众号
客服 反馈
顶部