Ruby version


  • 1
    T
    class Stack
      # Initialize your data structure here.
    
      def initialize
        @queue = Queue.new
      end
    
      # @param {Integer} x
      # @return {void}
      def push(x)
        @queue.push x
        (@queue.size - 1).times do
          @queue.push @queue.pop
        end
      end
    
      # @return {void}
      def pop
        @queue.pop
      end
    
      # @return {Integer}
      def top
        q = @queue.pop
        push q
        return q
      end
    
      # @return {Boolean}
      def empty
        @queue.empty?
      end
    end
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.