Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 771983c

Browse files
committed
Update stack_queue.md
1 parent 7b21930 commit 771983c

File tree

1 file changed

+184
-1
lines changed

1 file changed

+184
-1
lines changed

‎data_structure/stack_queue.md‎

Lines changed: 184 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
## Stack 栈
1414

15-
### [/implement-queue-using-stacks](https://leetcode.cn/problems/implement-queue-using-stacks)
15+
### [implement-queue-using-stacks](https://leetcode.cn/problems/implement-queue-using-stacks)
1616
> 请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):
1717
> 实现 MyQueue 类:
1818
@@ -23,8 +23,154 @@
2323
> 说明:
2424
> 你 只能 使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
2525
> 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
26+
```python
27+
class MyQueue:
2628

29+
def __init__(self):
30+
self.stack = list()
2731

32+
def push(self, x: int) -> None:
33+
self.stack.append(x)
34+
35+
def pop(self) -> int:
36+
tmp = None
37+
if len(self.stack):
38+
tmp = self.stack[0]
39+
self.stack = self.stack[1:]
40+
return tmp
41+
42+
def peek(self) -> int:
43+
return self.stack[0]
44+
45+
def empty(self) -> bool:
46+
return len(self.stack) == 0
47+
48+
49+
# Your MyQueue object will be instantiated and called as such:
50+
# obj = MyQueue()
51+
# obj.push(x)
52+
# param_2 = obj.pop()
53+
# param_3 = obj.peek()
54+
# param_4 = obj.empty()
55+
```
56+
57+
### [/implement-stack-using-queues](https://leetcode.cn/problems/implement-stack-using-queues/)
58+
> 请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。
59+
```python
60+
class MyStack:
61+
62+
def __init__(self):
63+
self.deq = list()
64+
65+
def push(self, x: int) -> None:
66+
self.deq.append(x)
67+
68+
def pop(self) -> int:
69+
tmp = None
70+
if not self.empty():
71+
tmp = self.deq.pop()
72+
return tmp
73+
74+
def top(self) -> int:
75+
if self.empty():
76+
return None
77+
else:
78+
return self.deq[-1]
79+
80+
def empty(self) -> bool:
81+
return len(self.deq) == 0
82+
83+
84+
# Your MyStack object will be instantiated and called as such:
85+
# obj = MyStack()
86+
# obj.push(x)
87+
# param_2 = obj.pop()
88+
# param_3 = obj.top()
89+
# param_4 = obj.empty()
90+
```
91+
92+
用collections中的deuqe类
93+
```python
94+
from collections import deque
95+
class MyStack:
96+
97+
def __init__(self):
98+
self.deq = deque()
99+
100+
101+
def push(self, x: int) -> None:
102+
self.deq.appendleft(x)
103+
104+
def pop(self) -> int:
105+
tmp = None
106+
if len(self.deq):
107+
tmp = self.deq.popleft()
108+
return tmp
109+
110+
def top(self) -> int:
111+
if self.empty():
112+
return None
113+
else:
114+
return self.deq[0]
115+
116+
def empty(self) -> bool:
117+
return len(self.deq) == 0
118+
119+
120+
# Your MyStack object will be instantiated and called as such:
121+
# obj = MyStack()
122+
# obj.push(x)
123+
# param_2 = obj.pop()
124+
# param_3 = obj.top()
125+
# param_4 = obj.empty()
126+
```
127+
128+
### [valid-parentheses](https://leetcode.cn/problems/valid-parentheses)
129+
> 给定一个只包括 '(',')','{','}','[',']' 的字符串 s ,判断字符串是否有效。
130+
131+
> 有效字符串需满足:
132+
133+
> 左括号必须用相同类型的右括号闭合。
134+
> 左括号必须以正确的顺序闭合。
135+
> 每个右括号都有一个对应的相同类型的左括号。
136+
137+
括号匹配是栈的拿手好戏。
138+
```python
139+
class Solution:
140+
def paired(self, a, b):
141+
if (a == '(' and b == ')') or (a == '{' and b == '}' ) or ( a == '[' and b == ']' ):
142+
return True
143+
else:
144+
return False
145+
def isValid(self, s: str) -> bool:
146+
147+
s= list(s)
148+
if len(s) % 2 == 1: return False
149+
stack = list()
150+
for i, val in enumerate(s):
151+
if len(stack) == 0 or not self.paired(stack[-1], val):
152+
stack.append(val)
153+
else:
154+
stack.pop()
155+
156+
return len(stack) == 0
157+
```
158+
159+
### [remove-all-adjacent-duplicates-in-string](https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/)
160+
> 给出由小写字母组成的字符串 S,重复项删除操作会选择两个相邻且相同的字母,并删除它们。
161+
> 在 S 上反复执行重复项删除操作,直到无法继续删除。
162+
> 在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。
163+
```python
164+
class Solution:
165+
def removeDuplicates(self, s: str) -> str:
166+
stack = list()
167+
for val in s:
168+
if len(stack) and stack[-1] == val: stack.pop()
169+
else: stack.append(val)
170+
171+
return ''.join(stack)
172+
173+
```
28174

29175

30176
### [min-stack](https://leetcode-cn.com/problems/min-stack/)
@@ -103,6 +249,43 @@ int evalRPN(vector<string>& tokens) {
103249
}
104250
```
105251

252+
```python
253+
class Solution:
254+
def evalRPN(self, tokens: List[str]) -> int:
255+
stack = list()
256+
for val in tokens:
257+
if len(stack) > 0 and val in ['+', '-', '*', '/']:
258+
op1 = stack.pop(-1)
259+
op2 = stack.pop(-1)
260+
if val == '+': stack.append(op2 + op1)
261+
elif val == '-': stack.append(op2 - op1)
262+
elif val == '*': stack.append(op2 * op1)
263+
else: stack.append(int(op2 / op1))
264+
else:
265+
stack.append(int(val))
266+
267+
return int(stack.pop())
268+
```
269+
简洁点
270+
```python
271+
class Solution:
272+
def evalRPN(self, tokens: List[str]) -> int:
273+
stack = list()
274+
for val in tokens:
275+
if len(stack) > 0 and val in ['+', '-', '*', '/']:
276+
op1 = stack.pop(-1)
277+
op2 = stack.pop(-1)
278+
stack.append(int(eval(f'{op2}{val}{op1}')))
279+
else:
280+
stack.append(int(val))
281+
282+
return int(stack.pop())
283+
```
284+
285+
### [sliding-window-maximum]()
286+
287+
288+
106289
### [decode-string](https://leetcode-cn.com/problems/decode-string/)
107290

108291
> 给定一个经过编码的字符串,返回它解码后的字符串。

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /