---
# System prepended metadata

title: 225. Implement Stack using Queues
tags: [stack]

---

# 225. Implement Stack using Queues
###### tags: `stack`

https://leetcode.com/problems/implement-stack-using-queues/

## 思路
1. 為什麼？因為你就是要建立pop 所以你不能在呼叫pop阿，你需要手刻他，所以需要這行
` self.queue = self.queue[:-1]`
我直接用pop()不是一樣意思，還是這樣算是偷吃步，



## 我的解法

```python=
class MyStack:

    def __init__(self):
        self.queue=[]

    def push(self, x: int) -> None:
        self.queue.append(x)

    def pop(self) -> int:
        top = self.queue[-1]
        #self.queue = self.queue[:-1]
        self.queue.pop()
        return top
    
    def top(self) -> int:
        return self.queue[-1]    

    def empty(self) -> bool:
        return False if self.queue else True
    


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

## 網友解法


```python=
class MyStack:

    def __init__(self):
        self.queue=[]

    def push(self, x: int) -> None:
        self.queue.append(x)

    def pop(self) -> int:
        top = self.queue[-1]
        self.queue = self.queue[:-1]
        return top
    
    def top(self) -> int:
        return self.queue[-1]    

    def empty(self) -> bool:
        return False if self.queue else True
    


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