mirror of
https://github.com/krahets/hello-algo.git
synced 2026-02-10 14:15:37 +08:00
Modify the exception handling in Java and Python.
This commit is contained in:
@@ -90,7 +90,7 @@ class MaxHeap {
|
||||
public int pop() {
|
||||
// 判空处理
|
||||
if (isEmpty())
|
||||
throw new EmptyStackException();
|
||||
throw new IndexOutOfBoundsException();
|
||||
// 交换根节点与最右叶节点(即交换首元素与尾元素)
|
||||
swap(0, size() - 1);
|
||||
// 删除节点
|
||||
|
||||
@@ -89,14 +89,14 @@ class ArrayDeque {
|
||||
/* 访问队首元素 */
|
||||
public int peekFirst() {
|
||||
if (isEmpty())
|
||||
throw new EmptyStackException();
|
||||
throw new IndexOutOfBoundsException();
|
||||
return nums[front];
|
||||
}
|
||||
|
||||
/* 访问队尾元素 */
|
||||
public int peekLast() {
|
||||
if (isEmpty())
|
||||
throw new EmptyStackException();
|
||||
throw new IndexOutOfBoundsException();
|
||||
// 计算尾元素索引
|
||||
int last = index(front + queSize - 1);
|
||||
return nums[last];
|
||||
|
||||
@@ -60,7 +60,7 @@ class ArrayQueue {
|
||||
/* 访问队首元素 */
|
||||
public int peek() {
|
||||
if (isEmpty())
|
||||
throw new EmptyStackException();
|
||||
throw new IndexOutOfBoundsException();
|
||||
return nums[front];
|
||||
}
|
||||
|
||||
|
||||
@@ -35,14 +35,14 @@ class ArrayStack {
|
||||
/* 出栈 */
|
||||
public int pop() {
|
||||
if (isEmpty())
|
||||
throw new EmptyStackException();
|
||||
throw new IndexOutOfBoundsException();
|
||||
return stack.remove(size() - 1);
|
||||
}
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
public int peek() {
|
||||
if (isEmpty())
|
||||
throw new EmptyStackException();
|
||||
throw new IndexOutOfBoundsException();
|
||||
return stack.get(size() - 1);
|
||||
}
|
||||
|
||||
|
||||
@@ -56,7 +56,7 @@ class LinkedListQueue {
|
||||
/* 访问队首元素 */
|
||||
public int peek() {
|
||||
if (size() == 0)
|
||||
throw new EmptyStackException();
|
||||
throw new IndexOutOfBoundsException();
|
||||
return front.val;
|
||||
}
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ class LinkedListStack {
|
||||
/* 访问栈顶元素 */
|
||||
public int peek() {
|
||||
if (size() == 0)
|
||||
throw new EmptyStackException();
|
||||
throw new IndexOutOfBoundsException();
|
||||
return stackPeek.val;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user