Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4ab3d5c

Browse files
refactor: 数组切片实现栈和队列
1 parent 6c23cd1 commit 4ab3d5c

File tree

1 file changed

+37
-51
lines changed

1 file changed

+37
-51
lines changed

‎02-链表、栈、队列、递归、哈希表、顺序表.md‎

Lines changed: 37 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -261,75 +261,61 @@ func (q *Queue)IsEmpty() bool {
261261
}
262262
```
263263

264-
> 数组实现栈和队列, 对于栈特别简单,对于队列,如下
264+
> 数组实现栈和队列, 对于栈特别简单,略过,对于队列,如下
265265
266-
```Java
267-
package class02;
268-
269-
public class Code04_RingArray {
270-
271-
public static class MyQueue {
272-
// 数组结构
273-
private int[] arr;
274-
// 往当前队列添加数的下标位置
275-
private int pushi;
276-
// 当前队列需要出队列的位置
277-
private int polli;
278-
// 当前队列使用的空间大小
279-
private int size;
280-
// 数组最大大小,用户传入
281-
private final int limit;
282-
283-
public MyQueue(int limit) {
284-
arr = new int[limit];
285-
pushi = 0;
286-
polli = 0;
287-
size = 0;
288-
this.limit = limit;
289-
}
290266

291-
public void push(int value) {
292-
if (size == limit) {
293-
throw new RuntimeException("栈满了,不能再加了");
294-
}
295-
size++;
296-
arr[pushi] = value;
297-
pushi = nextIndex(pushi);
298-
}
267+
```Go
268+
package main
299269

300-
public int pop() {
301-
if (size == 0) {
302-
throw new RuntimeException("栈空了,不能再拿了");
303-
}
304-
size--;
305-
int ans = arr[polli];
306-
polli = nextIndex(polli);
307-
return ans;
308-
}
270+
import "fmt"
309271

310-
public boolean isEmpty() {
311-
return size == 0;
312-
}
272+
type Que struct {
273+
// 队列的底层结构
274+
arr []int
275+
}
313276

314-
// 如果现在的下标是i,返回下一个位置,该实现可以实现环形的ringbuffer
315-
private int nextIndex(int i) {
316-
return i < limit - 1 ? i + 1 : 0;
317-
}
277+
func (q *Que) push (v int) {
278+
q.arr = append(q.arr, v)
279+
}
318280

281+
func (q *Que) poll () (int, bool){
282+
if len(q.arr) == 0 {
283+
return 0, false
319284
}
285+
v := q.arr[0]
286+
q.arr = q.arr[1:]
287+
return v, true
288+
}
320289

290+
func main () {
291+
q := Que{}
292+
q.push(1)
293+
q.push(9)
294+
q.push(3)
295+
if poll, ok := q.poll(); ok {
296+
fmt.Println(poll)
297+
}
298+
if poll, ok := q.poll(); ok {
299+
fmt.Println(poll)
300+
}
301+
if poll, ok := q.poll(); ok {
302+
fmt.Println(poll)
303+
}
304+
if poll, ok := q.poll(); ok {
305+
fmt.Println(poll)
306+
}
321307
}
322308
```
323309

324310
## 1.3 栈、队列常见面试题
325311

326-
一、实现一个特殊的栈,在基本功能的基础上,再实现返回栈中最小元素的功能更
312+
一、实现一个特殊的栈,在基本功能的基础上,再实现返回栈中最小元素的功能
327313

328314
1、pop、push、getMin操作的时间复杂度都是O(1)
329315

330316
2、设计的栈类型可以使用现成的栈结构
331317

332-
> 思路:准备两个栈,一个data栈,一个min栈。数据压data栈,min栈对比min栈顶元素,谁小加谁。这样的话data栈和min栈是同步上升的,元素个数一样多,且min栈的栈顶,是data栈所有元素中最小的那个。数据弹出data栈,我们同步弹出min栈,保证个数相等,切min栈弹出的就是最小值
318+
> 思路:准备两个栈,一个data栈,一个min栈。数据压data栈,min栈对比min栈顶元素,谁小加谁。这样的话data栈和min栈是同步上升的,元素个数一样多,且min栈的栈顶,是data栈所有元素中最小的那个。数据弹出data栈,我们同步弹出min栈,保证个数相等,且min栈弹出的就是最小值
333319
334320
```Java
335321
package class02;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /