Skip to content

Commit 100b39b

Browse files
committed
problem007
1 parent a58d944 commit 100b39b

File tree

6 files changed

+270
-1
lines changed

6 files changed

+270
-1
lines changed
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# 题意
2+
题目描述
3+
4+
> 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
5+
6+
#分析
7+
始终维护s1作为存储空间,以s2作为临时缓冲区。
8+
9+
始终维护s1作为输入栈,以s2作为输出栈
10+
11+
- 入队时,将元素压入s1。
12+
- 出队时,判断s2是否为空,如不为空,则直接弹出顶元素;如为空,则将s1的元素逐个“倒入”s2,把最后一个元素弹出并出队。 这个思路,避免了反复“倒”栈,仅在需要时才“倒”一次。但在实际面试中很少有人说出,可能是时间较少的缘故吧。
13+
14+
15+
## 解题思路
16+
17+
见程序注释
18+
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package problem007
2+
3+
import (
4+
"fmt"
5+
"errors"
6+
"../utils"
7+
)
8+
9+
type Queue struct {
10+
in utils.Stack
11+
out utils.Stack
12+
}
13+
14+
func (q *Queue) IsEmpty() bool {
15+
return q.in.IsEmpty() && q.out.IsEmpty()
16+
}
17+
18+
func (q *Queue) Push(value interface{}) {
19+
q.in.Push(value)
20+
}
21+
22+
func (q *Queue) Pop() (interface{}, error) {
23+
if q.IsEmpty() {
24+
return nil, errors.New("Queue is empty")
25+
}
26+
27+
var value interface{}
28+
29+
if !q.out.IsEmpty() {
30+
value, _ = q.out.Pop()
31+
return value, nil
32+
}
33+
34+
for !q.in.IsEmpty() {
35+
value, _ = q.in.Pop()
36+
q.out.Push(value)
37+
}
38+
value, _ = q.out.Pop()
39+
return value, nil
40+
}
41+
42+
43+
func main() {
44+
var myQueue Queue
45+
46+
myQueue.Push(1)
47+
myQueue.Push(2)
48+
fmt.Println(myQueue.Pop())
49+
fmt.Println(myQueue.Pop())
50+
myQueue.Push(3)
51+
myQueue.Push(4)
52+
fmt.Println(myQueue.Pop())
53+
fmt.Println(myQueue.Pop())
54+
fmt.Println(myQueue.Pop())
55+
}
56+
57+
58+
59+
60+
61+
62+
63+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package problem007
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestQueue_IsEmpty(t *testing.T) {
8+
var myQueue Queue
9+
myQueue.Push(1)
10+
myQueue.Push("test")
11+
if myQueue.IsEmpty() == false {
12+
t.Log("Pass Queue.IsEmpty")
13+
} else {
14+
t.Error("Failed Queue.IsEmpty")
15+
}
16+
}
17+
18+
func TestQueue_Push(t *testing.T) {
19+
var mQueue Queue
20+
mQueue.Push(3)
21+
mQueue.Push(4)
22+
mQueue.Push(5)
23+
mQueue.Push(6)
24+
if mQueue.IsEmpty() == false {
25+
t.Log("Pass Queue.Push")
26+
} else {
27+
t.Error("Failed Queue.Push")
28+
}
29+
}
30+
31+
func TestQueue_Pop(t *testing.T) {
32+
var mQueue Queue
33+
if _, err := mQueue.Pop(); err == nil {
34+
t.Error("Failed Queue.Pop")
35+
}
36+
37+
mQueue.Push(3)
38+
mQueue.Push(4)
39+
40+
41+
if value, _ := mQueue.Pop(); value == 3 {
42+
t.Log("Pass Queue.Pop")
43+
} else {
44+
t.Errorf("Failed Queue.Pop, value should be %d", 3)
45+
}
46+
if value, _ := mQueue.Pop(); value == 4 {
47+
t.Log("Pass Queue.Pop")
48+
} else {
49+
t.Errorf("Failed Queue.Pop, value should be %d", 4)
50+
}
51+
52+
if _, err := mQueue.Pop(); err == nil {
53+
t.Error("Failed Queue.Pop")
54+
}
55+
56+
mQueue.Push(5)
57+
58+
if value, _ := mQueue.Pop(); value == 5 {
59+
t.Log("Pass Queue.Pop")
60+
} else {
61+
t.Errorf("Failed Queue.Pop, value should be %d", 5)
62+
}
63+
64+
if _, err := mQueue.Pop(); err == nil {
65+
t.Error("Failed Queue.Pop")
66+
}
67+
68+
69+
}

README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# 剑指Offer - Golang实现
22

3+
若题目对应包内有 \*\_test.go 文件,可直接执行go test进行单元测试
4+
```shell
5+
go test
6+
```
7+
8+
若没有test文件,请直接执行
9+
```shell
10+
go run problemXXX.go
11+
```
12+
313
| 题目 |
414
| ---------- |
515
| [003-二维数组中的查找](https://github.com/DinghaoLI/Coding-Interviews-Golang/tree/master/003-%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84%E6%9F%A5%E6%89%BE) |
@@ -65,7 +75,7 @@
6575
| [063-二叉搜索树的第K个结点](https://github.com/DinghaoLI/Coding-Interviews-Golang/tree/master/063-%e4%ba%8c%e5%8f%89%e6%90%9c%e7%b4%a2%e6%a0%91%e7%9a%84%e7%ac%acK%e4%b8%aa%e7%bb%93%e7%82%b9) |
6676
| [064-数据流之中的中位数](https://github.com/DinghaoLI/Coding-Interviews-Golang/tree/master/064-%e6%95%b0%e6%8d%ae%e6%b5%81%e4%b9%8b%e4%b8%ad%e7%9a%84%e4%b8%ad%e4%bd%8d%e6%95%b0) |
6777
| [065-滑动窗口的最大值](https://github.com/DinghaoLI/Coding-Interviews-Golang/tree/master/065-%e6%bb%91%e5%8a%a8%e7%aa%97%e5%8f%a3%e7%9a%84%e6%9c%80%e5%a4%a7%e5%80%bc) |
68-
|
78+
6979

7080

7181
如有错误或者更好的版本,欢迎提交意见

utils/stack.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package utils
2+
3+
import "errors"
4+
5+
type Stack []interface {}
6+
7+
func (stack Stack) Len() int {
8+
return len(stack)
9+
}
10+
11+
func (stack Stack) IsEmpty() bool {
12+
return len(stack) == 0
13+
}
14+
15+
func (stack Stack) Cap() int {
16+
return cap(stack)
17+
}
18+
19+
func (stack *Stack) Push(value interface{}) {
20+
*stack = append(*stack, value)
21+
}
22+
23+
func (stack Stack) Top() (interface{}, error) {
24+
if len(stack) == 0 {
25+
return nil, errors.New("Out of index, len is 0")
26+
}
27+
return stack[len(stack) - 1], nil
28+
}
29+
30+
func (stack *Stack) Pop() (interface{}, error) {
31+
theStack := *stack
32+
if len(theStack) == 0 {
33+
return nil, errors.New("Out of index, len is 0")
34+
}
35+
value := theStack[len(theStack) - 1]
36+
*stack = theStack[:len(theStack) - 1]
37+
return value, nil
38+
}

utils/stack_test.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package utils
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestStack_Len(t *testing.T) {
8+
var myStack Stack
9+
myStack.Push(1)
10+
myStack.Push("test")
11+
if myStack.Len() == 2 {
12+
t.Log("Pass Stack.Len")
13+
} else {
14+
t.Error("Failed Stack.Len")
15+
}
16+
}
17+
18+
func TestStack_IsEmpty(t *testing.T) {
19+
var mStack Stack
20+
if mStack.IsEmpty() {
21+
t.Log("Pass Stack.IsEmpty")
22+
} else {
23+
t.Error("Failed Stack.IsEmpty")
24+
}
25+
}
26+
27+
func TestStack_Cap(t *testing.T) {
28+
myStack := make(Stack, 3)
29+
if myStack.Cap() == 3 {
30+
t.Log("Pass Stack.Cap")
31+
} else {
32+
t.Error("Failed Stack.Cap")
33+
}
34+
}
35+
36+
func TestStack_Push(t *testing.T) {
37+
var mStack Stack
38+
mStack.Push(3)
39+
if mStack.Len() == 1 {
40+
t.Log("Pass Stack.Push")
41+
} else {
42+
t.Error("Failed Stack.Push")
43+
}
44+
}
45+
46+
func TestStack_Top(t *testing.T) {
47+
var mStack Stack
48+
if _, err := mStack.Top(); err == nil {
49+
t.Error("Failed Stack.Top")
50+
}
51+
mStack.Push(3)
52+
if value, _ := mStack.Top(); value == 3 {
53+
t.Log("Pass Stack.Top")
54+
} else {
55+
t.Errorf("Failed Stack.Top, value is %d", value)
56+
}
57+
}
58+
59+
func TestStack_Pop(t *testing.T) {
60+
var mStack Stack
61+
if _, err := mStack.Pop(); err == nil {
62+
t.Error("Failed Stack.Top")
63+
}
64+
mStack.Push("test")
65+
mStack.Push(3)
66+
if value, _ := mStack.Pop(); value == 3 && mStack.Len() == 1 {
67+
t.Log("Pass Stack.Pop")
68+
} else {
69+
t.Errorf("Failed Stack.Pop, value is %d, len is %d", value, mStack.Len())
70+
}
71+
}

0 commit comments

Comments
 (0)