leetcode 232 栈实现队列

2018-10-22  本文已影响0人  TomorrowWu

232. 用栈实现队列

使用栈实现队列的下列操作:

示例:

MyQueue queue = new MyQueue();

queue.push(1);
queue.push(2);
queue.peek();  // 返回 1
queue.pop();   // 返回 1
queue.empty(); // 返回 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();
 */

// MyQueue defines Stack1 queue by two stacks
type MyQueue struct {
    Stack1, Stack2 *stack
}

// Constructor Initialize your data structure here.
func Constructor() MyQueue {
    return MyQueue{
        Stack1: newStack(),
        Stack2: newStack(),
    }
}

// Push element x to the back of queue.
func (queue *MyQueue) Push(x int) {
    queue.Stack1.push(x)
}

// Pop Removes the element from in front of queue and returns that element.
func (queue *MyQueue) Pop() int {
    if queue.Stack2.isEmpty() {
        //优化: 栈a中留一个元素供pop,可以少一次操作
        for queue.Stack1.len() > 1 {
            queue.Stack2.push(queue.Stack1.pop())
        }
        return queue.Stack1.pop()
    }
    return queue.Stack2.pop()
}

// Peek Get the front element.
func (queue *MyQueue) Peek() int {
    if queue.Stack2.isEmpty() {
        if queue.Stack1.isEmpty() {
            return -1
        }
        return queue.Stack1.nums[0]
    }
    return queue.Stack2.nums[queue.Stack2.len()-1]
}

// Empty Returns whether the queue is empty.
func (queue *MyQueue) Empty() bool {
    return queue.Stack1.isEmpty() && queue.Stack2.isEmpty()
}

// stack defines Stack1 stack
type stack struct {
    nums []int
}

// newStack creates a empty stack
func newStack() *stack {
    return &stack{
        nums: []int{},
    }
}

func (s *stack) push(n int) {
    s.nums = append(s.nums, n)
}

func (s *stack) pop() int {
    if s.isEmpty() {
        return -1
    }
    res := s.nums[len(s.nums)-1]
    s.nums = s.nums[:len(s.nums)-1]
    return res
}

func (s *stack) len() int {
    return len(s.nums)
}
func (s *stack) isEmpty() bool {
    return s.len() == 0
}

GitHub

参考资料

用两个栈实现一个队列——我作为面试官的小结

上一篇下一篇

猜你喜欢

热点阅读