都很简单不赘述
type MyQueue struct {StackinTop intStackOutTop intStackIn []intStackOut []int }func Constructor() MyQueue {StackIn := make([]int,0)StackOut := make([]int,0)return MyQueue{StackinTop: 0,StackOutTop:0,StackIn:StackIn,StackOut:StackOut,} }func (this *MyQueue) Push(x int) {this.StackIn = append(this.StackIn,x)this.StackinTop++ }func (this *MyQueue) Pop() int {tmp := this.StackIn[0]this.StackIn = this.StackIn[1:]return tmp }func (this *MyQueue) Peek() int {return this.StackIn[0] }func (this *MyQueue) Empty() bool {if len(this.StackIn) != 0{return false}else{return true} }/*** Your MyQueue object will be instantiated and called as such:* obj := Constructor();* obj.Push(x);* param_2 := obj.Pop();* param_3 := obj.Peek();* param_4 := obj.Empty();*/
type MyStack struct {Queue1 []intQueue2 []int }func Constructor() MyStack {queue1:= make([]int,0)queue2:= make([]int,0)return MyStack{Queue1:queue1,Queue2:queue2,} }func (this *MyStack) Push(x int) {this.Queue1 = append(this.Queue1,x) }func (this *MyStack) Pop() int {tmp := this.Queue1[len(this.Queue1) - 1]this.Queue1 = this.Queue1[0:len(this.Queue1) - 1]return tmp }func (this *MyStack) Top() int {return this.Queue1[len(this.Queue1)-1] }func (this *MyStack) Empty() bool {if len(this.Queue1) == 0{return true}return false }/*** Your MyStack object will be instantiated and called as such:* obj := Constructor();* obj.Push(x);* param_2 := obj.Pop();* param_3 := obj.Top();* param_4 := obj.Empty();*/
func isValid(s string) bool {stack := make([]rune, 0)runes := []rune(s)for _, i2 := range runes {if i2 == '(' || i2 == '[' || i2 == '{' {stack = append(stack, i2)} else {if len(stack) == 0|| (i2 == ')' && stack[len(stack)-1] != '(') || (i2 == '}' && stack[len(stack)-1] != '{') || (i2 == ']' && stack[len(stack)-1] != '['){return false}stack = stack[0 : len(stack)-1]}}if len(stack) != 0 {return false}return true }
func removeDuplicates(s string) string {stack := make([]rune,0)runes := []rune(s)for _,i2 := range runes{if len(stack) != 0 && stack[len(stack) - 1] == i2{stack = stack[0:len(stack) - 1]}else{stack = append(stack,i2)}}return string(stack) }