diff --git a/codes/cpp/chapter_array_and_linkedlist/array.cpp b/codes/cpp/chapter_array_and_linkedlist/array.cpp index fb620dbbd..22c5e50dd 100644 --- a/codes/cpp/chapter_array_and_linkedlist/array.cpp +++ b/codes/cpp/chapter_array_and_linkedlist/array.cpp @@ -30,7 +30,7 @@ int* extend(int* nums, int size, int enlarge) { /* 在数组的索引 index 处插入元素 num */ void insert(int* nums, int size, int num, int index) { // 把索引 index 以及之后的所有元素向后移动一位 - for (int i = size - 1; i >= index; i--) { + for (int i = size - 1; i > index; i--) { nums[i] = nums[i - 1]; } // 将 num 赋给 index 处元素 diff --git a/codes/cpp/chapter_stack_and_queue/array_stack.cpp b/codes/cpp/chapter_stack_and_queue/array_stack.cpp index 96a751035..fe5dc3110 100644 --- a/codes/cpp/chapter_stack_and_queue/array_stack.cpp +++ b/codes/cpp/chapter_stack_and_queue/array_stack.cpp @@ -29,18 +29,22 @@ public: /* 出栈 */ int pop() { - int oldTop = stack.back(); + int oldTop = top(); stack.pop_back(); return oldTop; } /* 访问栈顶元素 */ int top() { + if(empty()) + throw out_of_range("栈为空"); return stack.back(); } /* 访问索引 index 处元素 */ int get(int index) { + if(index >= size()) + throw out_of_range("索引越界"); return stack[index]; } diff --git a/codes/csharp/chapter_array_and_linkedlist/Array.cs b/codes/csharp/chapter_array_and_linkedlist/Array.cs index 72584c0af..cc6b79dd4 100644 --- a/codes/csharp/chapter_array_and_linkedlist/Array.cs +++ b/codes/csharp/chapter_array_and_linkedlist/Array.cs @@ -41,7 +41,7 @@ namespace hello_algo.chapter_array_and_linkedlist public static void Insert(int[] nums, int num, int index) { // 把索引 index 以及之后的所有元素向后移动一位 - for (int i = nums.Length - 1; i >= index; i--) + for (int i = nums.Length - 1; i > index; i--) { nums[i] = nums[i - 1]; } diff --git a/codes/java/chapter_array_and_linkedlist/array.java b/codes/java/chapter_array_and_linkedlist/array.java index 76b436dc1..6afd3dfdb 100644 --- a/codes/java/chapter_array_and_linkedlist/array.java +++ b/codes/java/chapter_array_and_linkedlist/array.java @@ -35,7 +35,7 @@ public class array { /* 在数组的索引 index 处插入元素 num */ static void insert(int[] nums, int num, int index) { // 把索引 index 以及之后的所有元素向后移动一位 - for (int i = nums.length - 1; i >= index; i--) { + for (int i = nums.length - 1; i > index; i--) { nums[i] = nums[i - 1]; } // 将 num 赋给 index 处元素 diff --git a/codes/java/chapter_stack_and_queue/array_stack.java b/codes/java/chapter_stack_and_queue/array_stack.java index 26a3e9605..44d0d70ce 100644 --- a/codes/java/chapter_stack_and_queue/array_stack.java +++ b/codes/java/chapter_stack_and_queue/array_stack.java @@ -33,16 +33,22 @@ class ArrayStack { /* 出栈 */ public int pop() { + if (isEmpty()) + throw new EmptyStackException(); return stack.remove(size() - 1); } /* 访问栈顶元素 */ public int peek() { + if (isEmpty()) + throw new EmptyStackException(); return stack.get(size() - 1); } /* 访问索引 index 处元素 */ public int get(int index) { + if (index >= size()) + throw new IndexOutOfBoundsException(); return stack.get(index); } diff --git a/codes/java/chapter_stack_and_queue/linkedlist_queue.java b/codes/java/chapter_stack_and_queue/linkedlist_queue.java index 291ff4174..92cdfab0e 100644 --- a/codes/java/chapter_stack_and_queue/linkedlist_queue.java +++ b/codes/java/chapter_stack_and_queue/linkedlist_queue.java @@ -57,7 +57,7 @@ class LinkedListQueue { /* 访问队首元素 */ public int peek() { if (size() == 0) - throw new IndexOutOfBoundsException(); + throw new EmptyStackException(); return front.val; } diff --git a/codes/java/chapter_stack_and_queue/linkedlist_stack.java b/codes/java/chapter_stack_and_queue/linkedlist_stack.java index 8b1d67b13..794c80c31 100644 --- a/codes/java/chapter_stack_and_queue/linkedlist_stack.java +++ b/codes/java/chapter_stack_and_queue/linkedlist_stack.java @@ -47,7 +47,7 @@ class LinkedListStack { /* 访问栈顶元素 */ public int peek() { if (size() == 0) - throw new IndexOutOfBoundsException(); + throw new EmptyStackException(); return stackPeek.val; } diff --git a/codes/javascript/chapter_array_and_linkedlist/array.js b/codes/javascript/chapter_array_and_linkedlist/array.js index 754e8fb15..191b6f3fa 100644 --- a/codes/javascript/chapter_array_and_linkedlist/array.js +++ b/codes/javascript/chapter_array_and_linkedlist/array.js @@ -30,7 +30,7 @@ function extend(nums, enlarge) { /* 在数组的索引 index 处插入元素 num */ function insert(nums, num, index) { // 把索引 index 以及之后的所有元素向后移动一位 - for (let i = nums.length - 1; i >= index; i--) { + for (let i = nums.length - 1; i > index; i--) { nums[i] = nums[i - 1]; } // 将 num 赋给 index 处元素 diff --git a/codes/javascript/chapter_stack_and_queue/array_stack.js b/codes/javascript/chapter_stack_and_queue/array_stack.js index a83d16f0e..271210bd1 100644 --- a/codes/javascript/chapter_stack_and_queue/array_stack.js +++ b/codes/javascript/chapter_stack_and_queue/array_stack.js @@ -28,16 +28,19 @@ class ArrayStack { /* 出栈 */ pop() { + if (this.empty()) throw "栈为空"; return this.stack.pop(); } /* 访问栈顶元素 */ top() { + if (this.empty()) throw "栈为空"; return this.stack[this.stack.length - 1]; } /* 访问索引 index 处元素 */ get(index) { + if (index >= this.size) throw "索引越界"; return this.stack[index]; } diff --git a/codes/python/chapter_array_and_linkedlist/array.py b/codes/python/chapter_array_and_linkedlist/array.py index bdfc54294..7f34472c7 100644 --- a/codes/python/chapter_array_and_linkedlist/array.py +++ b/codes/python/chapter_array_and_linkedlist/array.py @@ -31,7 +31,7 @@ def extend(nums, enlarge): """ 在数组的索引 index 处插入元素 num """ def insert(nums, num, index): # 把索引 index 以及之后的所有元素向后移动一位 - for i in range(len(nums) - 1, index - 1, -1): + for i in range(len(nums) - 1, index, -1): nums[i] = nums[i - 1] # 将 num 赋给 index 处元素 nums[index] = num diff --git a/codes/python/chapter_stack_and_queue/array_stack.py b/codes/python/chapter_stack_and_queue/array_stack.py index 23e4b4b5d..32ba9dcda 100644 --- a/codes/python/chapter_stack_and_queue/array_stack.py +++ b/codes/python/chapter_stack_and_queue/array_stack.py @@ -27,14 +27,17 @@ class ArrayStack: """ 出栈 """ def pop(self): + assert not self.is_empty(), "栈为空" return self.__stack.pop() """ 访问栈顶元素 """ def peek(self): + assert not self.is_empty(), "栈为空" return self.__stack[-1] """ 访问索引 index 处元素 """ def get(self, index): + assert index < self.size(), "索引越界" return self.__stack[index] """ 返回列表用于打印 """ diff --git a/codes/typescript/chapter_array_and_linkedlist/array.ts b/codes/typescript/chapter_array_and_linkedlist/array.ts index e73887905..237a04385 100644 --- a/codes/typescript/chapter_array_and_linkedlist/array.ts +++ b/codes/typescript/chapter_array_and_linkedlist/array.ts @@ -30,7 +30,7 @@ function extend(nums: number[], enlarge: number): number[] { /* 在数组的索引 index 处插入元素 num */ function insert(nums: number[], num: number, index: number): void { // 把索引 index 以及之后的所有元素向后移动一位 - for (let i = nums.length - 1; i >= index; i--) { + for (let i = nums.length - 1; i > index; i--) { nums[i] = nums[i - 1]; } // 将 num 赋给 index 处元素 diff --git a/codes/typescript/chapter_stack_and_queue/array_stack.ts b/codes/typescript/chapter_stack_and_queue/array_stack.ts index 5a57b6cf7..93a380f45 100644 --- a/codes/typescript/chapter_stack_and_queue/array_stack.ts +++ b/codes/typescript/chapter_stack_and_queue/array_stack.ts @@ -11,6 +11,7 @@ class ArrayStack { constructor() { this.stack = []; } + /* 获取栈的长度 */ get size(): number { return this.stack.length; @@ -28,16 +29,19 @@ class ArrayStack { /* 出栈 */ pop(): number | undefined { + if (this.empty()) throw new Error('栈为空'); return this.stack.pop(); } /* 访问栈顶元素 */ top(): number | undefined { + if (this.empty()) throw new Error('栈为空'); return this.stack[this.stack.length - 1]; } /* 访问索引 index 处元素 */ get(index: number): number | undefined { + if (index >= this.size) throw new Error('索引越界'); return this.stack[index]; } @@ -83,4 +87,4 @@ console.log("栈的长度 size = " + size); const empty = stack.empty(); console.log("栈是否为空 = " + empty); -export { }; \ No newline at end of file +export { }; diff --git a/docs/chapter_array_and_linkedlist/array.md b/docs/chapter_array_and_linkedlist/array.md index 392731639..70e30129a 100644 --- a/docs/chapter_array_and_linkedlist/array.md +++ b/docs/chapter_array_and_linkedlist/array.md @@ -314,7 +314,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex /* 在数组的索引 index 处插入元素 num */ void insert(int[] nums, int num, int index) { // 把索引 index 以及之后的所有元素向后移动一位 - for (int i = nums.length - 1; i >= index; i--) { + for (int i = nums.length - 1; i > index; i--) { nums[i] = nums[i - 1]; } // 将 num 赋给 index 处元素 @@ -336,7 +336,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex /* 在数组的索引 index 处插入元素 num */ void insert(int* nums, int size, int num, int index) { // 把索引 index 以及之后的所有元素向后移动一位 - for (int i = size - 1; i >= index; i--) { + for (int i = size - 1; i > index; i--) { nums[i] = nums[i - 1]; } // 将 num 赋给 index 处元素 @@ -358,7 +358,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex """ 在数组的索引 index 处插入元素 num """ def insert(nums, num, index): # 把索引 index 以及之后的所有元素向后移动一位 - for i in range(len(nums) - 1, index - 1, -1): + for i in range(len(nums) - 1, index, -1): nums[i] = nums[i - 1] # 将 num 赋给 index 处元素 nums[index] = num @@ -382,7 +382,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex /* 在数组的索引 index 处插入元素 num */ function insert(nums, num, index) { // 把索引 index 以及之后的所有元素向后移动一位 - for (let i = nums.length - 1; i >= index; i--) { + for (let i = nums.length - 1; i > index; i--) { nums[i] = nums[i - 1]; } // 将 num 赋给 index 处元素 @@ -404,7 +404,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex /* 在数组的索引 index 处插入元素 num */ function insert(nums: number[], num: number, index: number): void { // 把索引 index 以及之后的所有元素向后移动一位 - for (let i = nums.length - 1; i >= index; i--) { + for (let i = nums.length - 1; i > index; i--) { nums[i] = nums[i - 1]; } // 将 num 赋给 index 处元素 @@ -433,7 +433,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex void Insert(int[] nums, int num, int index) { // 把索引 index 以及之后的所有元素向后移动一位 - for (int i = nums.Length - 1; i >= index; i--) + for (int i = nums.Length - 1; i > index; i--) { nums[i] = nums[i - 1]; } diff --git a/docs/chapter_stack_and_queue/queue.md b/docs/chapter_stack_and_queue/queue.md index e431c62ee..229c6c94a 100644 --- a/docs/chapter_stack_and_queue/queue.md +++ b/docs/chapter_stack_and_queue/queue.md @@ -264,7 +264,7 @@ comments: true /* 访问队首元素 */ public int peek() { if (size() == 0) - throw new IndexOutOfBoundsException(); + throw new EmptyStackException(); return front.val; } } diff --git a/docs/chapter_stack_and_queue/stack.md b/docs/chapter_stack_and_queue/stack.md index d81f8f2b6..b78d79320 100644 --- a/docs/chapter_stack_and_queue/stack.md +++ b/docs/chapter_stack_and_queue/stack.md @@ -225,7 +225,6 @@ comments: true class LinkedListStack { private ListNode stackPeek; // 将头结点作为栈顶 private int stkSize = 0; // 栈的长度 - public LinkedListStack() { stackPeek = null; } @@ -254,7 +253,7 @@ comments: true /* 访问栈顶元素 */ public int peek() { if (size() == 0) - throw new IndexOutOfBoundsException(); + throw new EmptyStackException(); return stackPeek.val; } } @@ -448,14 +447,20 @@ comments: true } /* 出栈 */ public int pop() { + if (isEmpty()) + throw new EmptyStackException(); return stack.remove(size() - 1); } /* 访问栈顶元素 */ public int peek() { + if (isEmpty()) + throw new EmptyStackException(); return stack.get(size() - 1); } /* 访问索引 index 处元素 */ public int get(int index) { + if (index >= size()) + throw new EmptyStackException(); return stack.get(index); } } @@ -484,16 +489,20 @@ comments: true } /* 出栈 */ int pop() { - int oldTop = stack.back(); + int oldTop = top(); stack.pop_back(); return oldTop; } /* 访问栈顶元素 */ int top() { + if(empty()) + throw out_of_range("栈为空"); return stack.back(); } /* 访问索引 index 处元素 */ int get(int index) { + if(index >= size()) + throw out_of_range("索引越界"); return stack[index]; } }; @@ -521,14 +530,17 @@ comments: true """ 出栈 """ def pop(self): + assert not self.is_empty(), "栈为空" return self.__stack.pop() """ 访问栈顶元素 """ def peek(self): + assert not self.is_empty(), "栈为空" return self.__stack[-1] """ 访问索引 index 处元素 """ def get(self, index): + assert index < self.size(), "索引越界" return self.__stack[index] ``` @@ -539,30 +551,25 @@ comments: true type ArrayStack struct { data []int // 数据 } - func NewArrayStack() *ArrayStack { return &ArrayStack{ // 设置栈的长度为 0,容量为 16 data: make([]int, 0, 16), } } - // Size 栈的长度 func (s *ArrayStack) Size() int { return len(s.data) } - // IsEmpty 栈是否为空 func (s *ArrayStack) IsEmpty() bool { return s.Size() == 0 } - // Push 入栈 func (s *ArrayStack) Push(v int) { // 切片会自动扩容 s.data = append(s.data, v) } - // Pop 出栈 func (s *ArrayStack) Pop() any { // 弹出栈前,先判断是否为空 @@ -573,7 +580,6 @@ comments: true s.data = s.data[:len(s.data)-1] return val } - // Peek 获取栈顶元素 func (s *ArrayStack) Peek() any { if s.IsEmpty() { @@ -597,36 +603,29 @@ comments: true get size() { return this.stack.length; } - /* 判断栈是否为空 */ empty() { return this.stack.length === 0; } - /* 入栈 */ push(num) { this.stack.push(num); } - /* 出栈 */ pop() { + if (this.empty()) throw "栈为空"; return this.stack.pop(); } - /* 访问栈顶元素 */ top() { + if (this.empty()) throw "栈为空"; return this.stack[this.stack.length - 1]; } - /* 访问索引 index 处元素 */ get(index) { + if (index >= this.size) throw "索引越界"; return this.stack[index]; } - - /* 返回 Array */ - toArray() { - return this.stack; - } }; ``` @@ -643,36 +642,29 @@ comments: true get size(): number { return this.stack.length; } - /* 判断栈是否为空 */ empty(): boolean { return this.stack.length === 0; } - /* 入栈 */ push(num: number): void { this.stack.push(num); } - /* 出栈 */ pop(): number | undefined { + if (empty()) throw new Error('栈为空'); return this.stack.pop(); } - /* 访问栈顶元素 */ top(): number | undefined { + if (empty()) throw new Error('栈为空'); return this.stack[this.stack.length - 1]; } - /* 访问索引 index 处元素 */ get(index: number): number | undefined { + if (index >= size()) throw new Error('索引越界'); return this.stack[index]; } - - /* 返回 Array */ - toArray() { - return this.stack; - } }; ```