diff --git a/.github/workflows/prettier.yml b/.github/workflows/prettier.yml index 6e5f5a36684d2..8855388f639e3 100644 --- a/.github/workflows/prettier.yml +++ b/.github/workflows/prettier.yml @@ -20,6 +20,6 @@ jobs: uses: creyD/prettier_action@v3.0 with: prettier_options: --write **/*.{js,c,cpp,go,rb,java,cs,py,scala,md} - commit_message: 'docs: prettify code' + commit_message: 'style: prettify code' env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/README.md b/README.md index aba2d095eb23d..b63e503348ddf 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,9 @@ ### 栈和队列 +1. [有效的括号](/solution/0000-0099/0020.Valid%20Parentheses/README.md) +1. [用栈实现队列](/solution/0200-0299/0232.Implement%20Queue%20using%20Stacks/README.md) + ### 动态规划 ### 混合问题 diff --git a/README_EN.md b/README_EN.md index 3d3b7a71bbb09..4f8b4ff631886 100644 --- a/README_EN.md +++ b/README_EN.md @@ -71,6 +71,9 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF ### Stack & Queue +1. [Valid Parentheses](/solution/0000-0099/0020.Valid%20Parentheses/README_EN.md) +1. [Implement Queue using Stacks](/solution/0200-0299/0232.Implement%20Queue%20using%20Stacks/README_EN.md) + ### Dynamic Programming ### Misc diff --git a/solution/0200-0299/0232.Implement Queue using Stacks/README.md b/solution/0200-0299/0232.Implement Queue using Stacks/README.md index 89ccbfa20f404..d3d6a8001fd57 100644 --- a/solution/0200-0299/0232.Implement Queue using Stacks/README.md +++ b/solution/0200-0299/0232.Implement Queue using Stacks/README.md @@ -43,7 +43,60 @@ queue.empty(); // 返回 false ```python - +class MyQueue: + + def __init__(self): + """ + Initialize your data structure here. + """ + self.s1 = [] + self.s2 = [] + + + def push(self, x: int) -> None: + """ + Push element x to the back of queue. + """ + self.s1.append(x) + + + def pop(self) -> int: + """ + Removes the element from in front of queue and returns that element. + """ + self._move() + return self.s2.pop() + + def peek(self) -> int: + """ + Get the front element. + """ + self._move() + return self.s2[-1] + + + def empty(self) -> bool: + """ + Returns whether the queue is empty. + """ + return len(self.s1) + len(self.s2) == 0 + + + def _move(self): + """ + Move elements from s1 to s2. + """ + if len(self.s2) == 0: + while len(self.s1) > 0: + self.s2.append(self.s1.pop()) + + +# 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() ``` ### **Java** @@ -51,7 +104,57 @@ queue.empty(); // 返回 false ```java - +class MyQueue { + + private Deque s1; + private Deque s2; + + /** Initialize your data structure here. */ + public MyQueue() { + s1 = new ArrayDeque<>(); + s2 = new ArrayDeque<>(); + } + + /** Push element x to the back of queue. */ + public void push(int x) { + s1.push(x); + } + + /** Removes the element from in front of queue and returns that element. */ + public int pop() { + move(); + return s2.pop(); + } + + /** Get the front element. */ + public int peek() { + move(); + return s2.peek(); + } + + /** Returns whether the queue is empty. */ + public boolean empty() { + return s1.isEmpty() && s2.isEmpty(); + } + + /** Move elements from s1 to s2. */ + private void move() { + if (s2.isEmpty()) { + while (!s1.isEmpty()) { + s2.push(s1.pop()); + } + } + } +} + +/** + * Your MyQueue object will be instantiated and called as such: + * MyQueue obj = new MyQueue(); + * obj.push(x); + * int param_2 = obj.pop(); + * int param_3 = obj.peek(); + * boolean param_4 = obj.empty(); + */ ``` ### **...** diff --git a/solution/0200-0299/0232.Implement Queue using Stacks/README_EN.md b/solution/0200-0299/0232.Implement Queue using Stacks/README_EN.md index 26a6752d70b8f..502ff35b8c050 100644 --- a/solution/0200-0299/0232.Implement Queue using Stacks/README_EN.md +++ b/solution/0200-0299/0232.Implement Queue using Stacks/README_EN.md @@ -39,13 +39,9 @@ queue.empty(); // returns false

Notes:

## Solutions @@ -55,13 +51,116 @@ queue.empty(); // returns false ### **Python3** ```python - +class MyQueue: + + def __init__(self): + """ + Initialize your data structure here. + """ + self.s1 = [] + self.s2 = [] + + + def push(self, x: int) -> None: + """ + Push element x to the back of queue. + """ + self.s1.append(x) + + + def pop(self) -> int: + """ + Removes the element from in front of queue and returns that element. + """ + self._move() + return self.s2.pop() + + def peek(self) -> int: + """ + Get the front element. + """ + self._move() + return self.s2[-1] + + + def empty(self) -> bool: + """ + Returns whether the queue is empty. + """ + return len(self.s1) + len(self.s2) == 0 + + + def _move(self): + """ + Move elements from s1 to s2. + """ + if len(self.s2) == 0: + while len(self.s1) > 0: + self.s2.append(self.s1.pop()) + + +# 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() ``` ### **Java** ```java - +class MyQueue { + + private Deque s1; + private Deque s2; + + /** Initialize your data structure here. */ + public MyQueue() { + s1 = new ArrayDeque<>(); + s2 = new ArrayDeque<>(); + } + + /** Push element x to the back of queue. */ + public void push(int x) { + s1.push(x); + } + + /** Removes the element from in front of queue and returns that element. */ + public int pop() { + move(); + return s2.pop(); + } + + /** Get the front element. */ + public int peek() { + move(); + return s2.peek(); + } + + /** Returns whether the queue is empty. */ + public boolean empty() { + return s1.isEmpty() && s2.isEmpty(); + } + + /** Move elements from s1 to s2. */ + private void move() { + if (s2.isEmpty()) { + while (!s1.isEmpty()) { + s2.push(s1.pop()); + } + } + } +} + +/** + * Your MyQueue object will be instantiated and called as such: + * MyQueue obj = new MyQueue(); + * obj.push(x); + * int param_2 = obj.pop(); + * int param_3 = obj.peek(); + * boolean param_4 = obj.empty(); + */ ``` ### **...** diff --git a/solution/0200-0299/0232.Implement Queue using Stacks/Solution.java b/solution/0200-0299/0232.Implement Queue using Stacks/Solution.java index 4fa69b8f9dd73..eb1090c2bb29a 100644 --- a/solution/0200-0299/0232.Implement Queue using Stacks/Solution.java +++ b/solution/0200-0299/0232.Implement Queue using Stacks/Solution.java @@ -1,45 +1,51 @@ - class MyQueue { +class MyQueue { - private Stack stack1 = new Stack<>(); - private Stack stack2 = new Stack<>(); + private Deque s1; + private Deque s2; - /** Initialize your data structure here. */ - public MyQueue() { - } - - /** Push element x to the back of queue. */ - public void push(int x) { - stack1.push(x); - } + /** Initialize your data structure here. */ + public MyQueue() { + s1 = new ArrayDeque<>(); + s2 = new ArrayDeque<>(); + } + + /** Push element x to the back of queue. */ + public void push(int x) { + s1.push(x); + } + + /** Removes the element from in front of queue and returns that element. */ + public int pop() { + move(); + return s2.pop(); + } + + /** Get the front element. */ + public int peek() { + move(); + return s2.peek(); + } + + /** Returns whether the queue is empty. */ + public boolean empty() { + return s1.isEmpty() && s2.isEmpty(); + } - /** Removes the element from in front of queue and returns that element. */ - public int pop() { - if (stack2.isEmpty()) { - if (stack1.isEmpty()) { - throw new IllegalStateException(); - } - while (!stack1.isEmpty()) { - stack2.push(stack1.pop()); - } + /** Move elements from s1 to s2. */ + private void move() { + if (s2.isEmpty()) { + while (!s1.isEmpty()) { + s2.push(s1.pop()); } - return stack2.pop(); } + } +} - /** Get the front element. */ - public int peek() { - if (stack2.isEmpty()) { - if (stack1.isEmpty()) { - throw new IllegalStateException(); - } - while (!stack1.isEmpty()) { - stack2.push(stack1.pop()); - } - } - return stack2.peek(); - } - - /** Returns whether the queue is empty. */ - public boolean empty() { - return stack1.isEmpty() && stack2.isEmpty(); - } - } \ No newline at end of file +/** + * Your MyQueue object will be instantiated and called as such: + * MyQueue obj = new MyQueue(); + * obj.push(x); + * int param_2 = obj.pop(); + * int param_3 = obj.peek(); + * boolean param_4 = obj.empty(); + */ \ No newline at end of file diff --git a/solution/0200-0299/0232.Implement Queue using Stacks/Solution.py b/solution/0200-0299/0232.Implement Queue using Stacks/Solution.py new file mode 100644 index 0000000000000..6029f85ec080b --- /dev/null +++ b/solution/0200-0299/0232.Implement Queue using Stacks/Solution.py @@ -0,0 +1,54 @@ +class MyQueue: + + def __init__(self): + """ + Initialize your data structure here. + """ + self.s1 = [] + self.s2 = [] + + + def push(self, x: int) -> None: + """ + Push element x to the back of queue. + """ + self.s1.append(x) + + + def pop(self) -> int: + """ + Removes the element from in front of queue and returns that element. + """ + self._move() + return self.s2.pop() + + def peek(self) -> int: + """ + Get the front element. + """ + self._move() + return self.s2[-1] + + + def empty(self) -> bool: + """ + Returns whether the queue is empty. + """ + return len(self.s1) + len(self.s2) == 0 + + + def _move(self): + """ + Move elements from s1 to s2. + """ + if len(self.s2) == 0: + while len(self.s1) > 0: + self.s2.append(self.s1.pop()) + + +# 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() \ No newline at end of file