-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathinterview_03.01_test.go
More file actions
70 lines (62 loc) · 1.62 KB
/
interview_03.01_test.go
File metadata and controls
70 lines (62 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//三合一。描述如何只用一个数组来实现三个栈。
//
// 你应该实现push(stackNum, value)、pop(stackNum)、isEmpty(stackNum)、peek(stackNum)方法。s
//tackNum表示栈下标,value表示压入的值。
//
// 构造函数会传入一个stackSize参数,代表每个栈的大小。
//
// 示例1:
//
// 输入:
//["TripleInOne", "push", "push", "pop", "pop", "pop", "isEmpty"]
//[[1], [0, 1], [0, 2], [0], [0], [0], [0]]
// 输出:
//[null, null, null, 1, -1, -1, true]
//说明:当栈为空时`pop, peek`返回-1,当栈满时`push`不压入元素。
//
//
// 示例2:
//
// 输入:
//["TripleInOne", "push", "push", "push", "pop", "pop", "pop", "peek"]
//[[2], [0, 1], [0, 2], [0, 3], [0], [0], [0], [0]]
// 输出:
//[null, null, null, null, 2, 1, -1, -1]
//
// Related Topics 设计
// 👍 34 👎 0
package leetcode
type TripleInOne struct {
list [][]int
stackSize int
}
func Constructor0301(stackSize int) TripleInOne {
return TripleInOne{
list: make([][]int, 3),
stackSize: stackSize,
}
}
func (this *TripleInOne) Push(stackNum int, value int) {
if len(this.list[stackNum]) == this.stackSize {
return
}
this.list[stackNum] = append(this.list[stackNum], value)
}
func (this *TripleInOne) Pop(stackNum int) int {
n := len(this.list[stackNum])
v := this.Peek(stackNum)
if n > 0 {
this.list[stackNum] = this.list[stackNum][:n-1]
}
return v
}
func (this *TripleInOne) Peek(stackNum int) int {
n := len(this.list[stackNum])
if n > 0 {
return this.list[stackNum][n-1]
}
return -1
}
func (this *TripleInOne) IsEmpty(stackNum int) bool {
return len(this.list[stackNum]) == 0
}