krahets 7 months ago
parent f748af6aa4
commit 1b29b658bf

@ -37,28 +37,28 @@ comments: true
from collections import deque
# 初始化双向队列
deque: deque[int] = deque()
deq: deque[int] = deque()
# 元素入队
deque.append(2) # 添加至队尾
deque.append(5)
deque.append(4)
deque.appendleft(3) # 添加至队首
deque.appendleft(1)
deq.append(2) # 添加至队尾
deq.append(5)
deq.append(4)
deq.appendleft(3) # 添加至队首
deq.appendleft(1)
# 访问元素
front: int = deque[0] # 队首元素
rear: int = deque[-1] # 队尾元素
front: int = deq[0] # 队首元素
rear: int = deq[-1] # 队尾元素
# 元素出队
pop_front: int = deque.popleft() # 队首元素出队
pop_rear: int = deque.pop() # 队尾元素出队
pop_front: int = deq.popleft() # 队首元素出队
pop_rear: int = deq.pop() # 队尾元素出队
# 获取双向队列的长度
size: int = len(deque)
size: int = len(deq)
# 判断双向队列是否为空
is_empty: bool = len(deque) == 0
is_empty: bool = len(deq) == 0
```
=== "C++"

@ -173,7 +173,7 @@ comments: true
"""列表容量"""
return len(self._tree)
def val(self, i: int) -> int:
def val(self, i: int) -> int | None:
"""获取索引为 i 节点的值"""
# 若索引越界,则返回 None ,代表空位
if i < 0 or i >= self.size():

@ -37,28 +37,28 @@ Similarly, we can directly use the double-ended queue classes implemented in pro
from collections import deque
# Initialize the deque
deque: deque[int] = deque()
deq: deque[int] = deque()
# Enqueue elements
deque.append(2) # Add to the tail
deque.append(5)
deque.append(4)
deque.appendleft(3) # Add to the head
deque.appendleft(1)
deq.append(2) # Add to the tail
deq.append(5)
deq.append(4)
deq.appendleft(3) # Add to the head
deq.appendleft(1)
# Access elements
front: int = deque[0] # The first element
rear: int = deque[-1] # The last element
front: int = deq[0] # The first element
rear: int = deq[-1] # The last element
# Dequeue elements
pop_front: int = deque.popleft() # The first element dequeued
pop_rear: int = deque.pop() # The last element dequeued
pop_front: int = deq.popleft() # The first element dequeued
pop_rear: int = deq.pop() # The last element dequeued
# Get the length of the deque
size: int = len(deque)
size: int = len(deq)
# Check if the deque is empty
is_empty: bool = len(deque) == 0
is_empty: bool = len(deq) == 0
```
=== "C++"

@ -171,7 +171,7 @@ The following code implements a binary tree based on array representation, inclu
"""列表容量"""
return len(self._tree)
def val(self, i: int) -> int:
def val(self, i: int) -> int | None:
"""获取索引为 i 节点的值"""
# 若索引越界,则返回 None ,代表空位
if i < 0 or i >= self.size():

@ -37,28 +37,28 @@ comments: true
from collections import deque
# 初始化雙向佇列
deque: deque[int] = deque()
deq: deque[int] = deque()
# 元素入列
deque.append(2) # 新增至佇列尾
deque.append(5)
deque.append(4)
deque.appendleft(3) # 新增至佇列首
deque.appendleft(1)
deq.append(2) # 新增至佇列尾
deq.append(5)
deq.append(4)
deq.appendleft(3) # 新增至佇列首
deq.appendleft(1)
# 訪問元素
front: int = deque[0] # 佇列首元素
rear: int = deque[-1] # 佇列尾元素
front: int = deq[0] # 佇列首元素
rear: int = deq[-1] # 佇列尾元素
# 元素出列
pop_front: int = deque.popleft() # 佇列首元素出列
pop_rear: int = deque.pop() # 佇列尾元素出列
pop_front: int = deq.popleft() # 佇列首元素出列
pop_rear: int = deq.pop() # 佇列尾元素出列
# 獲取雙向佇列的長度
size: int = len(deque)
size: int = len(deq)
# 判斷雙向佇列是否為空
is_empty: bool = len(deque) == 0
is_empty: bool = len(deq) == 0
```
=== "C++"

@ -173,7 +173,7 @@ comments: true
"""串列容量"""
return len(self._tree)
def val(self, i: int) -> int:
def val(self, i: int) -> int | None:
"""獲取索引為 i 節點的值"""
# 若索引越界,則返回 None ,代表空位
if i < 0 or i >= self.size():

Loading…
Cancel
Save