题型:单选题
以下代码用于检查字符串中的括号是否匹配,横线上应填写( )。
def is_balanced(s):
stack = []
for c in s:
if c in '([{':
stack.append(c)
else:
if not stack:
return False
top = stack.pop()
if (c == ')' and top != '(') or \
(c == ']' and top != '[') or \
(c == '}' and top != '{'):
return False
__________________ | A. True |
B. False |
| C. return stack |
D. return not stack |
更新时间:2025-06-20 13:24:40
|
【知识点】
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
