less than 1 minute read

20. Valid Parentheses (easy)

stack

class Solution:
    def isValid(self, s: str) -> bool:
        stack = []
        open_ = '({['
        close_ = ']})'
        for p in s:
            if p in open_:
                stack.append(p)
            if p in close_:
                c = ''
                if stack:
                    c = stack.pop()
                if c == '[' and p == ']':
                    continue
                if c == '(' and p == ')':
                    continue
                if c == '{' and p == '}':
                    continue
                return False
        return len(stack) == 0

232. Implement Queue using Stacks (easy)

class MyQueue:

    def __init__(self):
        self.data = []
        self.top = 0
        self.size = 0

    def push(self, x: int) -> None:
        self.data.append(x)
        self.size += 1

    def pop(self) -> int:
        if self.size <= 0:
            return -1
        temp = self.data[self.top]
        self.top += 1
        self.size -= 1
        return temp

    def peek(self) -> int:
        return self.data[self.top]

    def empty(self) -> bool:
        return self.size == 0
        


# Your MyQueue object will be instantiated and called as such:
# obj = MyQueue()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.peek()
# param_4 = obj.empty()