mirror of
https://github.com/krahets/hello-algo.git
synced 2026-02-11 14:45:38 +08:00
build
This commit is contained in:
@@ -416,9 +416,8 @@ comments: true
|
||||
|
||||
def pop(self, is_front: bool) -> int:
|
||||
"""出队操作"""
|
||||
# 若队列为空,直接返回 None
|
||||
if self.is_empty():
|
||||
return None
|
||||
raise IndexError("双向队列为空")
|
||||
# 队首出队操作
|
||||
if is_front:
|
||||
val: int = self.front.val # 暂存头节点值
|
||||
@@ -450,11 +449,15 @@ comments: true
|
||||
|
||||
def peek_first(self) -> int:
|
||||
"""访问队首元素"""
|
||||
return None if self.is_empty() else self.front.val
|
||||
if self.is_empty():
|
||||
raise IndexError("双向队列为空")
|
||||
return self.front.val
|
||||
|
||||
def peek_last(self) -> int:
|
||||
"""访问队尾元素"""
|
||||
return None if self.is_empty() else self.rear.val
|
||||
if self.is_empty():
|
||||
raise IndexError("双向队列为空")
|
||||
return self.rear.val
|
||||
|
||||
def to_array(self) -> list[int]:
|
||||
"""返回数组用于打印"""
|
||||
@@ -544,9 +547,8 @@ comments: true
|
||||
|
||||
/* 出队操作 */
|
||||
int pop(bool isFront) {
|
||||
// 若队列为空,直接返回 -1
|
||||
if (isEmpty())
|
||||
return -1;
|
||||
throw out_of_range("队列为空");
|
||||
int val;
|
||||
// 队首出队操作
|
||||
if (isFront) {
|
||||
@@ -587,12 +589,16 @@ comments: true
|
||||
|
||||
/* 访问队首元素 */
|
||||
int peekFirst() {
|
||||
return isEmpty() ? -1 : front->val;
|
||||
if (isEmpty())
|
||||
throw out_of_range("双向队列为空");
|
||||
return front->val;
|
||||
}
|
||||
|
||||
/* 访问队尾元素 */
|
||||
int peekLast() {
|
||||
return isEmpty() ? -1 : rear->val;
|
||||
if (isEmpty())
|
||||
throw out_of_range("双向队列为空");
|
||||
return rear->val;
|
||||
}
|
||||
|
||||
/* 返回数组用于打印 */
|
||||
@@ -675,10 +681,9 @@ comments: true
|
||||
}
|
||||
|
||||
/* 出队操作 */
|
||||
private Integer pop(boolean isFront) {
|
||||
// 若队列为空,直接返回 null
|
||||
private int pop(boolean isFront) {
|
||||
if (isEmpty())
|
||||
return null;
|
||||
throw new IndexOutOfBoundsException();
|
||||
int val;
|
||||
// 队首出队操作
|
||||
if (isFront) {
|
||||
@@ -706,23 +711,27 @@ comments: true
|
||||
}
|
||||
|
||||
/* 队首出队 */
|
||||
public Integer popFirst() {
|
||||
public int popFirst() {
|
||||
return pop(true);
|
||||
}
|
||||
|
||||
/* 队尾出队 */
|
||||
public Integer popLast() {
|
||||
public int popLast() {
|
||||
return pop(false);
|
||||
}
|
||||
|
||||
/* 访问队首元素 */
|
||||
public Integer peekFirst() {
|
||||
return isEmpty() ? null : front.val;
|
||||
public int peekFirst() {
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
return front.val;
|
||||
}
|
||||
|
||||
/* 访问队尾元素 */
|
||||
public Integer peekLast() {
|
||||
return isEmpty() ? null : rear.val;
|
||||
public int peekLast() {
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
return rear.val;
|
||||
}
|
||||
|
||||
/* 返回数组用于打印 */
|
||||
@@ -812,34 +821,29 @@ comments: true
|
||||
|
||||
/* 出队操作 */
|
||||
private int? pop(bool isFront) {
|
||||
// 若队列为空,直接返回 null
|
||||
if (isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (isEmpty())
|
||||
throw new Exception();
|
||||
int val;
|
||||
// 队首出队操作
|
||||
if (isFront) {
|
||||
val = front.val; // 暂存头节点值
|
||||
// 删除头节点
|
||||
// 删除头节点
|
||||
ListNode fNext = front.next;
|
||||
if (fNext != null) {
|
||||
fNext.prev = null;
|
||||
front.next = null;
|
||||
}
|
||||
|
||||
front = fNext; // 更新头节点
|
||||
}
|
||||
// 队尾出队操作
|
||||
else {
|
||||
val = rear.val; // 暂存尾节点值
|
||||
// 删除尾节点
|
||||
// 删除尾节点
|
||||
ListNode rPrev = rear.prev;
|
||||
if (rPrev != null) {
|
||||
rPrev.next = null;
|
||||
rear.prev = null;
|
||||
}
|
||||
|
||||
rear = rPrev; // 更新尾节点
|
||||
}
|
||||
|
||||
@@ -859,12 +863,16 @@ comments: true
|
||||
|
||||
/* 访问队首元素 */
|
||||
public int? peekFirst() {
|
||||
return isEmpty() ? null : front.val;
|
||||
if (isEmpty())
|
||||
throw new Exception();
|
||||
return front.val;
|
||||
}
|
||||
|
||||
/* 访问队尾元素 */
|
||||
public int? peekLast() {
|
||||
return isEmpty() ? null : rear.val;
|
||||
if (isEmpty())
|
||||
throw new Exception();
|
||||
return rear.val;
|
||||
}
|
||||
|
||||
/* 返回数组用于打印 */
|
||||
|
||||
@@ -358,9 +358,8 @@ comments: true
|
||||
|
||||
def peek(self) -> int:
|
||||
"""访问队首元素"""
|
||||
if self.size() == 0:
|
||||
print("队列为空")
|
||||
return False
|
||||
if self.is_empty():
|
||||
raise IndexError("队列为空")
|
||||
return self.__front.val
|
||||
|
||||
def to_list(self) -> list[int]:
|
||||
@@ -400,7 +399,7 @@ comments: true
|
||||
}
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
bool empty() {
|
||||
bool isEmpty() {
|
||||
return queSize == 0;
|
||||
}
|
||||
|
||||
@@ -502,7 +501,7 @@ comments: true
|
||||
|
||||
/* 访问队首元素 */
|
||||
public int peek() {
|
||||
if (size() == 0)
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
return front.val;
|
||||
}
|
||||
@@ -570,7 +569,7 @@ comments: true
|
||||
|
||||
/* 访问队首元素 */
|
||||
public int peek() {
|
||||
if (size() == 0 || front == null)
|
||||
if (isEmpty())
|
||||
throw new Exception();
|
||||
return front.val;
|
||||
}
|
||||
@@ -1303,7 +1302,7 @@ comments: true
|
||||
}
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
bool empty() {
|
||||
bool isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
@@ -1331,7 +1330,7 @@ comments: true
|
||||
|
||||
/* 访问队首元素 */
|
||||
int peek() {
|
||||
if (empty())
|
||||
if (isEmpty())
|
||||
throw out_of_range("队列为空");
|
||||
return nums[front];
|
||||
}
|
||||
|
||||
@@ -344,16 +344,15 @@ comments: true
|
||||
|
||||
def pop(self) -> int:
|
||||
"""出栈"""
|
||||
num: int = self.peek()
|
||||
num = self.peek()
|
||||
self.__peek = self.__peek.next
|
||||
self.__size -= 1
|
||||
return num
|
||||
|
||||
def peek(self) -> int:
|
||||
"""访问栈顶元素"""
|
||||
# 判空处理
|
||||
if not self.__peek:
|
||||
return None
|
||||
if self.is_empty():
|
||||
raise IndexError("栈为空")
|
||||
return self.__peek.val
|
||||
|
||||
def to_list(self) -> list[int]:
|
||||
@@ -393,7 +392,7 @@ comments: true
|
||||
}
|
||||
|
||||
/* 判断栈是否为空 */
|
||||
bool empty() {
|
||||
bool isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
@@ -417,7 +416,7 @@ comments: true
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
int top() {
|
||||
if (size() == 0)
|
||||
if (isEmpty())
|
||||
throw out_of_range("栈为空");
|
||||
return stackTop->val;
|
||||
}
|
||||
@@ -475,7 +474,7 @@ comments: true
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
public int peek() {
|
||||
if (size() == 0)
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
return stackPeek.val;
|
||||
}
|
||||
@@ -525,9 +524,6 @@ comments: true
|
||||
|
||||
/* 出栈 */
|
||||
public int pop() {
|
||||
if (stackPeek == null)
|
||||
throw new Exception();
|
||||
|
||||
int num = peek();
|
||||
stackPeek = stackPeek.next;
|
||||
stkSize--;
|
||||
@@ -536,7 +532,7 @@ comments: true
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
public int peek() {
|
||||
if (size() == 0 || stackPeek == null)
|
||||
if (isEmpty())
|
||||
throw new Exception();
|
||||
return stackPeek.val;
|
||||
}
|
||||
@@ -1139,8 +1135,8 @@ comments: true
|
||||
}
|
||||
|
||||
/* 判断栈是否为空 */
|
||||
bool empty() {
|
||||
return stack.empty();
|
||||
bool isEmpty() {
|
||||
return stack.size() == 0;
|
||||
}
|
||||
|
||||
/* 入栈 */
|
||||
@@ -1156,7 +1152,7 @@ comments: true
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
int top() {
|
||||
if (empty())
|
||||
if (isEmpty())
|
||||
throw out_of_range("栈为空");
|
||||
return stack.back();
|
||||
}
|
||||
|
||||
@@ -25,3 +25,11 @@ comments: true
|
||||
!!! question "双向队列像是两个栈拼接在了一起,它的用途是什么?"
|
||||
|
||||
双向队列就像是栈和队列的组合,或者是两个栈拼在了一起。它表现的是栈 + 队列的逻辑,因此可以实现栈与队列的所有应用,并且更加灵活。
|
||||
|
||||
!!! question "撤销(undo)和反撤销(redo)具体是如何实现的?"
|
||||
|
||||
使用两个堆栈,栈 `A` 用于撤销,栈 `B` 用于反撤销。
|
||||
|
||||
1. 每当用户执行一个操作,将这个操作压入栈 `A` ,并清空栈 `B` 。
|
||||
2. 当用户执行“撤销”时,从栈 `A` 中弹出最近的操作,并将其压入栈 `B` 。
|
||||
3. 当用户执行“反撤销”时,从栈 `B` 中弹出最近的操作,并将其压入栈 `A` 。
|
||||
|
||||
Reference in New Issue
Block a user