今日题目:
今日总结
第一天刷栈和队列的题目,题目不难,但是实现的细节很丰富。算是对于之前栈和队列的复习了。栈实现队列不难,队列实现栈需要注意一个队列就能够实现。其中的Move操作需要仔细研究。
232. 用栈实现队列
要点
- 两个栈In Out倒来倒去实现先进先出即可
- 在从stackIn放元素到stackOut时是倒序放进去的
代码:
type MyQueue struct {
stackIn []int //输入栈
stackOut []int //输出栈
}
func Constructor() MyQueue {
return MyQueue{
stackIn: make([]int, 0),
stackOut: make([]int, 0),
}
}
func (this *MyQueue) Push(x int) {
this.stackIn = append(this.stackIn, x)
}
func (this *MyQueue) Pop() int {
inLen, outLen := len(this.stackIn), len(this.stackOut)
if outLen == 0 {
if inLen == 0 {
return -1
}
for i := inLen - 1; i >= 0; i-- { //要反向放入out
this.stackOut = append(this.stackOut, this.stackIn[i])
}
this.stackIn = []int{}
outLen = len(this.stackOut)
}
val := this.stackOut[outLen-1]
this.stackOut = this.stackOut[:outLen-1]
return val
}
func (this *MyQueue) Peek() int { //只需要访问 不需要弹出
val := this.Pop()
if val == -1 {
return -1
}
this.stackOut = append(this.stackOut, val)
return val
}
func (this *MyQueue) Empty() bool {
if len(this.stackIn) == 0 && len(this.stackOut) == 0{
return true
}
return false
}
/**
* 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();
*/
225. 用队列实现栈
要点:
- 这种自定义数据结构的题目不要局限于题目给出的方法。
- 使用两个队列时,要理解Move操作。实质上就是将queue1的元素反向放入queue2再放回去。
- 使用一个队列时,要点就是在弹出时将队列的末尾元素(即栈的top元素)之前的所有元素放置在末尾元素的后方从而让它成为队列的首位,就能进行pop了。
- 注意题目要求是使用队列的基本操作,所以不能够直接从队列末尾弹出元素。
代码:两个队列
type MyStack struct {
//创建两个队列
queue1 []int
queue2 []int //暂存数据
}
func Constructor() MyStack {
return MyStack{ //初始化
queue1:make([]int,0),
queue2:make([]int,0),
}
}
func (this *MyStack) Push(x int) {
this.queue2 = append(this.queue2, x)
this.Move()
}
func (this *MyStack) Move(){ // 作用是将元素从queue1反向放入queue2从而实现先进后出
if len(this.queue1) == 0{
//交换,queue1置为queue2,queue2置为空
this.queue1,this.queue2 = this.queue2,this.queue1
}else{
//queue1元素从头开始一个一个追加到queue2中
this.queue2 = append(this.queue2,this.queue1[0])
this.queue1 = this.queue1[1:] //去除第一个元素
this.Move() //重复
}
}
func (this *MyStack) Pop() int {
val := this.queue1[0]
this.queue1 = this.queue1[1:] //去除第一个元素
return val
}
func (this *MyStack) Top() int {
return this.queue1[0]
}
func (this *MyStack) Empty() bool {
return len(this.queue1) == 0
}
/**
* 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();
*/
代码: 一个队列
type MyStack struct {
queue []int
}
func Constructor() MyStack {
return MyStack{
queue: make([]int, 0),
}
}
func (this *MyStack) Push(x int) {
this.queue=append(this.queue,x)
}
func (this *MyStack) Pop() int {
n := len(this.queue)-1 //把最后一个元素之外的所有元素都取出来存放到最后元素的后面
for n != 0 {
temp := this.queue[0]
this.queue = this.queue[1:]
this.queue =append(this.queue, temp)
n--
}
val := this.queue[0]
this.queue = this.queue[1:]
return val
}
func (this *MyStack) Top() int {
temp := this.Pop()
this.queue =append(this.queue, temp)
return temp
}
func (this *MyStack) Empty() bool {
if len(this.queue) == 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();
*/