标签:
Implement the following operations of a stack using queues.
Notes:
push to back
, peek/pop from front
, size
, and is empty
operations are valid.
Update (2015-06-11):
The class name of the Java function had been updated to MyStack instead of Stack.
class Stack # Initialize your data structure here. def initialize @q = Array.new end # @param {Integer} x # @return {void} def push(x) @q << x end # @return {void} def pop (@q.length-1).times {@q << @q.shift} @q.shift end # @return {Integer} def top (@q.length-1).times {@q << @q.shift} t = @q.shift @q << t t end # @return {Boolean} def empty @q.empty? end end
Leetcode 225 Implement Stack using Queues
标签:
原文地址:http://www.cnblogs.com/lilixu/p/4571013.html