|
@@ -1,51 +1,56 @@
|
|
|
package main
|
|
|
|
|
|
+// MinStackOld ...
|
|
|
type MinStackOld struct {
|
|
|
val []int
|
|
|
min []int
|
|
|
top int
|
|
|
}
|
|
|
|
|
|
-/** initialize your data structure here. */
|
|
|
+// ConstructorOld /** initialize your data structure here. */
|
|
|
func ConstructorOld() MinStackOld {
|
|
|
s := MinStackOld{make([]int, 0), make([]int, 0), -1}
|
|
|
return s
|
|
|
}
|
|
|
|
|
|
-func (this *MinStackOld) Push(x int) {
|
|
|
- this.val = append(this.val, x)
|
|
|
- this.top++
|
|
|
+// Push ...
|
|
|
+func (stack *MinStackOld) Push(x int) {
|
|
|
+ stack.val = append(stack.val, x)
|
|
|
+ stack.top++
|
|
|
idx := 0
|
|
|
- for ; idx < this.top; idx++ {
|
|
|
- if x > this.val[idx] {
|
|
|
+ for ; idx < stack.top; idx++ {
|
|
|
+ if x > stack.val[idx] {
|
|
|
break
|
|
|
}
|
|
|
}
|
|
|
- this.min = append(this.min, 0)
|
|
|
- copy(this.min[idx+1:this.top+1], this.min[idx:this.top])
|
|
|
- this.min[idx] = x
|
|
|
+ stack.min = append(stack.min, 0)
|
|
|
+ copy(stack.min[idx+1:stack.top+1], stack.min[idx:stack.top])
|
|
|
+ stack.min[idx] = x
|
|
|
}
|
|
|
|
|
|
-func (this *MinStackOld) Pop() {
|
|
|
- if this.top != -1 {
|
|
|
+// Pop ...
|
|
|
+func (stack *MinStackOld) Pop() {
|
|
|
+ if stack.top != -1 {
|
|
|
idx := 0
|
|
|
- for ; idx < this.top+1; idx++ {
|
|
|
- if this.min[idx] == this.val[this.top] {
|
|
|
+ for ; idx < stack.top+1; idx++ {
|
|
|
+ if stack.min[idx] == stack.val[stack.top] {
|
|
|
break
|
|
|
}
|
|
|
}
|
|
|
- this.min = append(this.min[:idx], this.min[idx+1:]...)
|
|
|
- this.val = this.val[:this.top]
|
|
|
- this.top--
|
|
|
+ stack.min = append(stack.min[:idx], stack.min[idx+1:]...)
|
|
|
+ stack.val = stack.val[:stack.top]
|
|
|
+ stack.top--
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func (this *MinStackOld) Top() int {
|
|
|
- return this.val[this.top]
|
|
|
+// Top ...
|
|
|
+func (stack *MinStackOld) Top() int {
|
|
|
+ return stack.val[stack.top]
|
|
|
}
|
|
|
|
|
|
-func (this *MinStackOld) GetMin() int {
|
|
|
- return this.min[this.top]
|
|
|
+// GetMin ...
|
|
|
+func (stack *MinStackOld) GetMin() int {
|
|
|
+ return stack.min[stack.top]
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -57,43 +62,47 @@ func (this *MinStackOld) GetMin() int {
|
|
|
* param_4 := obj.GetMin();
|
|
|
*/
|
|
|
|
|
|
+// MinStack ...
|
|
|
type MinStack struct {
|
|
|
val []int
|
|
|
min []int
|
|
|
}
|
|
|
|
|
|
-/** initialize your data structure here. */
|
|
|
+// Constructor /** initialize your data structure here. */
|
|
|
func Constructor() MinStack {
|
|
|
return MinStack{}
|
|
|
}
|
|
|
|
|
|
-// important! min []int stores the "current miximum"
|
|
|
-func (this *MinStack) Push(x int) {
|
|
|
- this.val = append(this.val, x)
|
|
|
- if len(this.min) == 0 {
|
|
|
- this.min = append(this.min, x)
|
|
|
+// Push important! min []int stores the "current miximum"
|
|
|
+func (stack *MinStack) Push(x int) {
|
|
|
+ stack.val = append(stack.val, x)
|
|
|
+ if len(stack.min) == 0 {
|
|
|
+ stack.min = append(stack.min, x)
|
|
|
return
|
|
|
}
|
|
|
- m := this.min[len(this.min)-1]
|
|
|
+ m := stack.min[len(stack.min)-1]
|
|
|
if x < m {
|
|
|
- this.min = append(this.min, x)
|
|
|
+ stack.min = append(stack.min, x)
|
|
|
return
|
|
|
}
|
|
|
- this.min = append(this.min, m)
|
|
|
+ stack.min = append(stack.min, m)
|
|
|
}
|
|
|
|
|
|
-func (this *MinStack) Pop() {
|
|
|
- n := len(this.val) - 1
|
|
|
- this.val = this.val[:n]
|
|
|
- this.min = this.min[:n]
|
|
|
+// Pop ...
|
|
|
+func (stack *MinStack) Pop() {
|
|
|
+ n := len(stack.val) - 1
|
|
|
+ stack.val = stack.val[:n]
|
|
|
+ stack.min = stack.min[:n]
|
|
|
}
|
|
|
|
|
|
-func (this *MinStack) Top() int {
|
|
|
- return this.val[len(this.val)-1]
|
|
|
+// Top ...
|
|
|
+func (stack *MinStack) Top() int {
|
|
|
+ return stack.val[len(stack.val)-1]
|
|
|
}
|
|
|
|
|
|
-func (this *MinStack) GetMin() int {
|
|
|
- return this.min[len(this.min)-1]
|
|
|
+// GetMin ...
|
|
|
+func (stack *MinStack) GetMin() int {
|
|
|
+ return stack.min[len(stack.min)-1]
|
|
|
}
|
|
|
|
|
|
// func main() {
|