栈与队列

用队列实现栈

leetcode 225 用队列实现栈
使用队列实现栈的下列操作:
push(x) – 元素 x 入栈
pop() – 移除栈顶元素
top() – 获取栈顶元素
empty() – 返回栈是否为空
注意:
你只能使用队列的基本操作-- 也就是 push to back, peek/pop from front, size, 和 is empty 这些操作是合法的。
你所使用的语言也许不支持队列。 你可以使用 list 或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
你可以假设所有操作都是有效的(例如, 对一个空的栈不会调用 pop 或者 top 操作)。
思路:
在这里插入图片描述
golang代码实现:
定义两个辅助队列 queue1 与 queue2,使用一个变量 top_element 记录栈顶元素。
push():
将元素入队 queue2,此时 queue2 中的首个元素为栈顶元素
更新 top_element
此时若 queue1 不为空,则让 queue1 中的元素逐个出队并加入 queue2 中
pop(): queue1 首个元素出队,更新 top_element
top(): 返回 top_element
empty(): 判断 queue1 长度

package main

/**
 * 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 {
	Queue1     []int
	Queue2     []int
	TopElement int
}

/** Initialize your data structure here. */
func Constructor() MyStack {
	return MyStack{}
}

/** Push element x onto stack. */
func (this *MyStack) Push(x int) {
	this.Queue2 = append(this.Queue2, x)
	this.TopElement = x
	if !this.Empty() {
        len:=len(this.Queue1)
		for i := 0; i < len; i++ {
			this.Queue2 = append(this.Queue2, this.Queue1[0])
            		this.Queue1 = this.Queue1[1:] //去除队头
		}
	}
	this.Queue1 = this.Queue2
	this.Queue2 = make([]int, 0)
}

/** Removes the element on top of the stack and returns that element. */
func (this *MyStack) Pop() int {
	top := this.Queue1[0]
	this.Queue1 = this.Queue1[1:]
	if !this.Empty() {
		//更新栈顶元素
		this.TopElement = this.Queue1[0]
	}
	return top
}

/** Get the top element. */
func (this *MyStack) Top() int {
	return this.TopElement
}

/** Returns whether the stack is empty. */
func (this *MyStack) Empty() bool {
	return len(this.Queue1) == 0
}

栈实现队列

leetcode 232 使用栈实现队列的下列操作:
push(x) – 将一个元素放入队列的尾部。
pop() – 从队列首部移除元素。
peek() – 返回队列首部的元素。
empty() – 返回队列是否为空。
示例:
MyQueue queue = new MyQueue();
queue.push(1);
queue.push(2);
queue.peek(); // 返回 1
queue.pop(); // 返回 1
queue.empty(); // 返回 false
在这里插入图片描述
go语言实现

type MyQueue struct {
	stack1 []int
	stack2 []int
	top    int
}

/** Initialize your data structure here. */
func Constructor1() MyQueue {
	return MyQueue{}
}

/** Push element x to the back of queue. */
func (this *MyQueue) Push(x int) {
	if !this.Empty() {
		for i := len(this.stack1) - 1; i >= 0; i-- {
			this.stack2 = append(this.stack2, this.stack1[i])
			this.stack1 = this.stack1[:i]
		}
	}
	this.stack2 = append(this.stack2, x)
	if len(this.stack2) > 0 {
		for i := len(this.stack2) - 1; i >= 0; i-- {
			this.stack1 = append(this.stack1, this.stack2[i])
		}
	}
	this.stack2 = make([]int, 0)
	this.top = this.stack1[len(this.stack1)-1]
}

/** Removes the element from in front of queue and returns that element. */
func (this *MyQueue) Pop() int {
	x := this.stack1[len(this.stack1)-1]
	this.stack1 = this.stack1[:len(this.stack1)-1]
	if !this.Empty() {
		this.top = this.stack1[len(this.stack1)-1]
	} else {
		this.top = 0
	}
	return x
}

/** Get the front element. */
func (this *MyQueue) Peek() int {
	return this.top
}

/** Returns whether the queue is empty. */
func (this *MyQueue) Empty() bool {
	return len(this.stack1) == 0
}

leetcode-面试题-30 包含min函数的栈

定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的 min 函数在该栈中,调用 min、push 及 pop 的时间复杂度都是 O(1)。
示例:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.min(); --> 返回 -3.
minStack.pop();
minStack.top(); --> 返回 0.
minStack.min(); --> 返回 -2.
思路:
引入辅助栈,最小值栈
当push值大于最小值栈顶时,该值等于最小值栈顶,再push进最小值栈
当push值小于最小值栈顶时,该值push进最小值栈
在这里插入图片描述

golang代码如下

type MinStack struct {
	dataStack []int
	minStack  []int
}

/** initialize your data structure here. */
func Constructor() MinStack {
	return MinStack{}
}

func (this *MinStack) Push(x int) {
	this.dataStack = append(this.dataStack, x)

	if len(this.minStack) > 0 {
		if x > this.Min() {
			x = this.Min()
		}
		this.minStack = append(this.minStack, x)
	} else {
		this.minStack = append(this.minStack, x)
	}
}

func (this *MinStack) Pop() {
	if len(this.dataStack) > 0 {
		this.dataStack = this.dataStack[:len(this.dataStack)-1]
	}
	if len(this.minStack) > 0 {
		this.minStack = this.minStack[:len(this.minStack)-1]
	}
}

func (this *MinStack) Top() int {
	return this.dataStack[len(this.dataStack)-1]
}

func (this *MinStack) Min() int {
	return this.minStack[len(this.minStack)-1]
}

面试题31. 栈的压入、弹出序列

输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如,序列 {1,2,3,4,5} 是某栈的压栈序列,序列 {4,5,3,2,1} 是该压栈序列对应的一个弹出序列,但 {4,3,5,1,2} 就不可能是该压栈序列的弹出序列。
示例 1:
输入:pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
输出:true
解释:我们可以按以下顺序执行:
push(1), push(2), push(3), push(4), pop() -> 4,
push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
示例 2:
输入:pushed = [1,2,3,4,5], popped = [4,3,5,1,2]
输出:false
解释:1 不能在 2 之前弹出。
在这里插入图片描述
golang实现

func validateStackSequences(pushed []int, popped []int) bool {
    var stack myStack
	for _, v := range pushed {
	    stack.Push(v)
		for v == popped[0] {
			stack.Pop()
			popped = popped[1:]
			if len(stack.stack) == 0 {
				break
			}
			v = stack.Top()
		}
	}

	if len(stack.stack) == 0 {
		return true
	}
	return false
}

type myStack struct {
	stack []int
}

func (this *myStack) Push(x int) {
	this.stack = append(this.stack, x)
}

func (this *myStack) Pop() {
	this.stack = this.stack[:len(this.stack)-1]
}

func (this *myStack) Top() int {
	return this.stack[len(this.stack)-1]
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值