Python solution without deque, just for fun, anyone make some suggestions?


  • -3
    L

    class Stack:
    # initialize your data structure here.
    def init(self):
    self.queue1 = []
    self.queue2 = []
    self.size = 0

    # @param x, an integer
    # @return nothing
    def push(self, x):
        if len(self.queue1)==0:
            self.queue1.append(x)
            for i in range(len(self.queue2)):
                self.queue1.append(self.queue2[i])
            self.queue2 = []
        else:
            self.queue2.append(x)
            for i in range(len(self.queue1)):
                self.queue2.append(self.queue1[i])
            self.queue1 = []
        self.size += 1
    
    # @return nothing
    def pop(self):
        if len(self.queue1)!=0:
            for i in range(len(self.queue1)-1):
                self.queue1[i] = self.queue1[i+1]
            self.queue1.pop()
        else:
            for i in range(len(self.queue2)-1):
                self.queue2[i] = self.queue2[i+1]
            self.queue2.pop()
        self.size -= 1
    
    # @return an integer
    def top(self):
        if len(self.queue1)!=0:
            return self.queue1[0]
        else:
            return self.queue2[0]
    
    # @return an boolean
    def empty(self):
        return self.size == 0

  • 0

    This makes no sense whatsoever. It violates the requirements, defeats the entire purpose of the challenge, and does so in an unbelievably complicated and inefficient way.


  • 0
    L

    Yeah, U r right :)


Log in to reply
 

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