|
14 | 14 |
|
15 | 15 | <pre>
|
16 | 16 |
|
17 |
| -<strong> Input</strong>: |
| 17 | +<strong> Input</strong>: |
18 | 18 |
|
19 | 19 | ["TripleInOne", "push", "push", "pop", "pop", "pop", "isEmpty"]
|
20 | 20 |
|
21 | 21 | [[1], [0, 1], [0, 2], [0], [0], [0], [0]]
|
22 | 22 |
|
23 |
| -<strong> Output</strong>: |
| 23 | +<strong> Output</strong>: |
24 | 24 |
|
25 | 25 | [null, null, null, 1, -1, -1, true]
|
26 | 26 |
|
|
32 | 32 |
|
33 | 33 | <pre>
|
34 | 34 |
|
35 |
| -<strong> Input</strong>: |
| 35 | +<strong> Input</strong>: |
36 | 36 |
|
37 | 37 | ["TripleInOne", "push", "push", "push", "pop", "pop", "pop", "peek"]
|
38 | 38 |
|
39 | 39 | [[2], [0, 1], [0, 2], [0, 3], [0], [0], [0], [0]]
|
40 | 40 |
|
41 |
| -<strong> Output</strong>: |
| 41 | +<strong> Output</strong>: |
42 | 42 |
|
43 | 43 | [null, null, null, null, 2, 1, -1, -1]
|
44 | 44 |
|
@@ -125,6 +125,68 @@ class TripleInOne {
|
125 | 125 | */
|
126 | 126 | ```
|
127 | 127 |
|
| 128 | +### **Go** |
| 129 | + |
| 130 | +```go |
| 131 | +type TripleInOne struct { |
| 132 | + data []int |
| 133 | + offset [3]int |
| 134 | + stackSize int |
| 135 | +} |
| 136 | + |
| 137 | +func Constructor(stackSize int) TripleInOne { |
| 138 | + total := stackSize * 3 |
| 139 | + data := make([]int, total) |
| 140 | + offset := [3]int{} |
| 141 | + for i := 0; i < 3; i++ { |
| 142 | + offset[i] = i * stackSize |
| 143 | + } |
| 144 | + return TripleInOne{ |
| 145 | + data: data, |
| 146 | + offset: offset, |
| 147 | + stackSize: stackSize, |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +func (this *TripleInOne) Push(stackNum int, value int) { |
| 152 | + i := this.offset[stackNum] |
| 153 | + if i < (stackNum+1)*this.stackSize { |
| 154 | + this.data[i] = value |
| 155 | + this.offset[stackNum]++ |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +func (this *TripleInOne) Pop(stackNum int) int { |
| 160 | + i := this.offset[stackNum] |
| 161 | + if i == stackNum*this.stackSize { |
| 162 | + return -1 |
| 163 | + } |
| 164 | + this.offset[stackNum]-- |
| 165 | + return this.data[i-1] |
| 166 | +} |
| 167 | + |
| 168 | +func (this *TripleInOne) Peek(stackNum int) int { |
| 169 | + i := this.offset[stackNum] |
| 170 | + if i == stackNum*this.stackSize { |
| 171 | + return -1 |
| 172 | + } |
| 173 | + return this.data[i-1] |
| 174 | +} |
| 175 | + |
| 176 | +func (this *TripleInOne) IsEmpty(stackNum int) bool { |
| 177 | + return this.offset[stackNum] == stackNum*this.stackSize |
| 178 | +} |
| 179 | + |
| 180 | +/** |
| 181 | + * Your TripleInOne object will be instantiated and called as such: |
| 182 | + * obj := Constructor(stackSize); |
| 183 | + * obj.Push(stackNum,value); |
| 184 | + * param_2 := obj.Pop(stackNum); |
| 185 | + * param_3 := obj.Peek(stackNum); |
| 186 | + * param_4 := obj.IsEmpty(stackNum); |
| 187 | + */ |
| 188 | +``` |
| 189 | + |
128 | 190 | ### **...**
|
129 | 191 |
|
130 | 192 | ```
|
|
0 commit comments