Skip to content

Commit df61663

Browse files
committed
feat: add python and java solutions to leetcode problem: No.0225
1 parent ca9bcb0 commit df61663

File tree

11 files changed

+579
-107
lines changed

11 files changed

+579
-107
lines changed

.github/workflows/prettier.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ jobs:
2020
uses: creyD/prettier_action@v3.0
2121
with:
2222
prettier_options: --write **/*.{js,c,cpp,go,rb,java,cs,py,scala,md}
23-
commit_message: 'docs: prettify code'
23+
commit_message: 'style: prettify code'
2424
env:
2525
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@
7676

7777
### 栈和队列
7878

79+
1. [有效的括号](/solution/0000-0099/0020.Valid%20Parentheses/README.md)
80+
1. [用栈实现队列](/solution/0200-0299/0232.Implement%20Queue%20using%20Stacks/README.md)
81+
1. [用队列实现栈](/solution/0200-0299/0225.Implement%20Stack%20using%20Queues/README.md)
82+
7983
### 动态规划
8084

8185
### 混合问题

README_EN.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
7171

7272
### Stack & Queue
7373

74+
1. [Valid Parentheses](/solution/0000-0099/0020.Valid%20Parentheses/README_EN.md)
75+
1. [Implement Queue using Stacks](/solution/0200-0299/0232.Implement%20Queue%20using%20Stacks/README_EN.md)
76+
1. [Implement Stack using Queues](/solution/0200-0299/0225.Implement%20Stack%20using%20Queues/README_EN.md)
77+
7478
### Dynamic Programming
7579

7680
### Misc

solution/0200-0299/0225.Implement Stack using Queues/README.md

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,102 @@
3333
<!-- 这里可写当前语言的特殊实现逻辑 -->
3434

3535
```python
36+
class MyStack:
3637

38+
def __init__(self):
39+
"""
40+
Initialize your data structure here.
41+
"""
42+
self.q = []
43+
44+
45+
def push(self, x: int) -> None:
46+
"""
47+
Push element x onto stack.
48+
"""
49+
self.q.append(x)
50+
n = len(self.q)
51+
for i in range(1, n):
52+
self.q.append(self.q.pop(0))
53+
54+
55+
def pop(self) -> int:
56+
"""
57+
Removes the element on top of the stack and returns that element.
58+
"""
59+
return self.q.pop(0)
60+
61+
62+
def top(self) -> int:
63+
"""
64+
Get the top element.
65+
"""
66+
return self.q[0]
67+
68+
69+
def empty(self) -> bool:
70+
"""
71+
Returns whether the stack is empty.
72+
"""
73+
return len(self.q) == 0
74+
75+
76+
77+
# Your MyStack object will be instantiated and called as such:
78+
# obj = MyStack()
79+
# obj.push(x)
80+
# param_2 = obj.pop()
81+
# param_3 = obj.top()
82+
# param_4 = obj.empty()
3783
```
3884

3985
### **Java**
4086

4187
<!-- 这里可写当前语言的特殊实现逻辑 -->
4288

4389
```java
44-
90+
class MyStack {
91+
92+
private Deque<Integer> q;
93+
94+
/** Initialize your data structure here. */
95+
public MyStack() {
96+
q = new ArrayDeque<>();
97+
}
98+
99+
/** Push element x onto stack. */
100+
public void push(int x) {
101+
q.offerLast(x);
102+
int n = q.size();
103+
while (n-- > 1) {
104+
q.offerLast(q.pollFirst());
105+
}
106+
}
107+
108+
/** Removes the element on top of the stack and returns that element. */
109+
public int pop() {
110+
return q.pollFirst();
111+
}
112+
113+
/** Get the top element. */
114+
public int top() {
115+
return q.peekFirst();
116+
}
117+
118+
/** Returns whether the stack is empty. */
119+
public boolean empty() {
120+
return q.isEmpty();
121+
}
122+
}
123+
124+
/**
125+
* Your MyStack object will be instantiated and called as such:
126+
* MyStack obj = new MyStack();
127+
* obj.push(x);
128+
* int param_2 = obj.pop();
129+
* int param_3 = obj.top();
130+
* boolean param_4 = obj.empty();
131+
*/
45132
```
46133

47134
### **...**

solution/0200-0299/0225.Implement Stack using Queues/README_EN.md

Lines changed: 88 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,10 @@
77
<p>Implement the following operations of a stack using queues.</p>
88

99
<ul>
10-
1110
<li>push(x) -- Push element x onto stack.</li>
12-
1311
<li>pop() -- Removes the element on top of the stack.</li>
14-
1512
<li>top() -- Get the top element.</li>
16-
1713
<li>empty() -- Return whether the stack is empty.</li>
18-
1914
</ul>
2015

2116
<p><b>Example:</b></p>
@@ -39,13 +34,9 @@ stack.empty(); // returns false</pre>
3934
<p><b>Notes:</b></p>
4035

4136
<ul>
42-
4337
<li>You must use <i>only</i> standard operations of a queue -- which means only <code>push to back</code>, <code>peek/pop from front</code>, <code>size</code>, and <code>is empty</code> operations are valid.</li>
44-
4538
<li>Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.</li>
46-
4739
<li>You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).</li>
48-
4940
</ul>
5041

5142
## Solutions
@@ -55,13 +46,100 @@ stack.empty(); // returns false</pre>
5546
### **Python3**
5647

5748
```python
49+
class MyStack:
50+
51+
def __init__(self):
52+
"""
53+
Initialize your data structure here.
54+
"""
55+
self.q = []
56+
5857

58+
def push(self, x: int) -> None:
59+
"""
60+
Push element x onto stack.
61+
"""
62+
self.q.append(x)
63+
n = len(self.q)
64+
for i in range(1, n):
65+
self.q.append(self.q.pop(0))
66+
67+
68+
def pop(self) -> int:
69+
"""
70+
Removes the element on top of the stack and returns that element.
71+
"""
72+
return self.q.pop(0)
73+
74+
75+
def top(self) -> int:
76+
"""
77+
Get the top element.
78+
"""
79+
return self.q[0]
80+
81+
82+
def empty(self) -> bool:
83+
"""
84+
Returns whether the stack is empty.
85+
"""
86+
return len(self.q) == 0
87+
88+
89+
90+
# Your MyStack object will be instantiated and called as such:
91+
# obj = MyStack()
92+
# obj.push(x)
93+
# param_2 = obj.pop()
94+
# param_3 = obj.top()
95+
# param_4 = obj.empty()
5996
```
6097

6198
### **Java**
6299

63100
```java
64-
101+
class MyStack {
102+
103+
private Deque<Integer> q;
104+
105+
/** Initialize your data structure here. */
106+
public MyStack() {
107+
q = new ArrayDeque<>();
108+
}
109+
110+
/** Push element x onto stack. */
111+
public void push(int x) {
112+
q.offerLast(x);
113+
int n = q.size();
114+
while (n-- > 1) {
115+
q.offerLast(q.pollFirst());
116+
}
117+
}
118+
119+
/** Removes the element on top of the stack and returns that element. */
120+
public int pop() {
121+
return q.pollFirst();
122+
}
123+
124+
/** Get the top element. */
125+
public int top() {
126+
return q.peekFirst();
127+
}
128+
129+
/** Returns whether the stack is empty. */
130+
public boolean empty() {
131+
return q.isEmpty();
132+
}
133+
}
134+
135+
/**
136+
* Your MyStack object will be instantiated and called as such:
137+
* MyStack obj = new MyStack();
138+
* obj.push(x);
139+
* int param_2 = obj.pop();
140+
* int param_3 = obj.top();
141+
* boolean param_4 = obj.empty();
142+
*/
65143
```
66144

67145
### **...**
Lines changed: 38 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,42 @@
1-
class MyStack {
1+
class MyStack {
22

3-
Queue<Integer> q1;
4-
Queue<Integer> q2;
3+
private Deque<Integer> q;
54

6-
/** Initialize your data structure here. */
7-
public MyStack() {
8-
q1 = new LinkedList<>();
9-
q2 = new LinkedList<>();
5+
/** Initialize your data structure here. */
6+
public MyStack() {
7+
q = new ArrayDeque<>();
8+
}
9+
10+
/** Push element x onto stack. */
11+
public void push(int x) {
12+
q.offerLast(x);
13+
int n = q.size();
14+
while (n-- > 1) {
15+
q.offerLast(q.pollFirst());
1016
}
17+
}
18+
19+
/** Removes the element on top of the stack and returns that element. */
20+
public int pop() {
21+
return q.pollFirst();
22+
}
23+
24+
/** Get the top element. */
25+
public int top() {
26+
return q.peekFirst();
27+
}
28+
29+
/** Returns whether the stack is empty. */
30+
public boolean empty() {
31+
return q.isEmpty();
32+
}
33+
}
1134

12-
/** Push element x onto stack. */
13-
public void push(int x) {
14-
q1.add(x);
15-
}
16-
17-
/** Removes the element on top of the stack and returns that element. */
18-
public int pop() {
19-
if (q1.isEmpty()) {
20-
return 0;
21-
}
22-
while (q1.size() > 1) {
23-
q2.add(q1.poll());
24-
}
25-
int result = q1.poll();
26-
Queue<Integer> temp = q1;
27-
q1 = q2;
28-
q2 = temp;
29-
return result;
30-
}
31-
32-
/** Get the top element. */
33-
public int top() {
34-
if (q1.isEmpty()) {
35-
return 0;
36-
}
37-
while (q1.size() > 1) {
38-
q2.add(q1.poll());
39-
}
40-
int result = q1.poll();
41-
q2.add(result);
42-
while (q2.size() > 0) {
43-
q1.add(q2.poll());
44-
}
45-
return result;
46-
}
47-
48-
/** Returns whether the stack is empty. */
49-
public boolean empty() {
50-
return q1.isEmpty() && q2.isEmpty();
51-
}
52-
}
35+
/**
36+
* Your MyStack object will be instantiated and called as such:
37+
* MyStack obj = new MyStack();
38+
* obj.push(x);
39+
* int param_2 = obj.pop();
40+
* int param_3 = obj.top();
41+
* boolean param_4 = obj.empty();
42+
*/

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy