Skip to content

Commit 65ca28f

Browse files
author
lucifer
committed
feat: 括号匹配系列
1 parent 8e86efe commit 65ca28f

File tree

2 files changed

+299
-49
lines changed

2 files changed

+299
-49
lines changed

problems/20.valid-parentheses.md

Lines changed: 90 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ Output: true
3939

4040
-
4141

42-
## 思路
42+
##
43+
44+
### 思路
4345

4446
关于这道题的思路,邓俊辉讲的非常好,没有看过的同学可以看一下,[视频地址](http://www.xuetangx.com/courses/course-v1:TsinghuaX+30240184+sp/courseware/ad1a23c053df4501a3facd66ef6ccfa9/8d6f450e7f7a445098ae1d507fda80f6/)
4547

@@ -64,17 +66,17 @@ Output: true
6466
6567
> 事实上,这类问题还可以进一步扩展,我们可以去解析类似 HTML 等标记语法, 比如 <p></p> <body></body>
6668
67-
## 关键点解析
69+
### 关键点解析
6870

6971
1. 栈的基本特点和操作
7072
2. 如果你用的是 JS 没有现成的栈,可以用数组来模拟
7173
入: push 出:pop
7274

7375
> 入: push 出 shift 就是队列
7476
75-
## 代码
77+
### 代码
7678

77-
- 语言支持:JS,Python
79+
代码支持:JS,Python
7880

7981
Javascript Code:
8082

@@ -112,7 +114,7 @@ var isValid = function (s) {
112114

113115
Python Code:
114116

115-
```
117+
```py
116118
class Solution:
117119
def isValid(self,s):
118120
stack = []
@@ -136,6 +138,88 @@ Python Code:
136138
return len(stack) == 0
137139
```
138140

141+
## O(1) 空间
142+
143+
### 思路
144+
145+
基本思路是修改参数,将参数作为我们的栈。 随着我们不断遍历, s 慢慢变成了一个栈。
146+
147+
因此 Python,Java,JS 等**字符串不可变**的语言无法使用此方法达到 $O(1)$。
148+
149+
具体参考: [No stack O(1) space complexity O(n) time complexity solution in C++](<https://leetcode.com/problems/valid-parentheses/discuss/9478/No-stack-O(1)-space-complexity-O(n)-time-complexity-solution-in-C++/244061>)
150+
151+
### 代码
152+
153+
代码支持:C++
154+
155+
C++:
156+
157+
```c++
158+
class Solution {
159+
public:
160+
bool isValid(string s) {
161+
int top = -1;
162+
for(int i =0;i<s.length();++i){
163+
if(top<0 || !isMatch(s[top], s[i])){
164+
++top;
165+
s[top] = s[i];
166+
}else{
167+
--top;
168+
}
169+
}
170+
return top == -1;
171+
}
172+
bool isMatch(char c1, char c2){
173+
if(c1 == '(' && c2 == ')') return true;
174+
if(c1 == '[' && c2 == ']') return true;
175+
if(c1 == '{' && c2 == '}') return true;
176+
return false;
177+
}
178+
};
179+
```
180+
181+
## 正则匹配
182+
183+
### 思路
184+
185+
我们不断通过消除 '[]' , '()', '{}' ,最后判断剩下的是否是空串即可,就像开心消消乐一样。
186+
187+
### 代码
188+
189+
代码支持:Python,JavaScript
190+
191+
Python:
192+
193+
```py
194+
class Solution:
195+
def isValid(self, s):
196+
197+
while '[]' in s or '()' in s or '{}' in s:
198+
s = s.replace('[]','').replace('()','').replace('{}','')
199+
return not len(s)
200+
```
201+
202+
JavaScript:
203+
204+
```js
205+
var isValid = function (s) {
206+
while (s.includes("[]") || s.includes("()") || s.includes("{}")) {
207+
s = s.replace("[]", "").replace("()", "").replace("{}", "");
208+
}
209+
s = s.replace("[]", "").replace("()", "").replace("{}", "");
210+
return s.length === 0;
211+
};
212+
```
213+
214+
**复杂度分析**
215+
216+
- 时间复杂度:取决于正则引擎的实现
217+
- 空间复杂度:取决于正则引擎的实现
218+
219+
## 相关题目
220+
221+
- [32. 最长有效括号](./32.longest-valid-parentheses.md)
222+
139223
## 扩展
140224

141-
如果让你检查 XML 标签是否闭合如何检查, 更进一步如果要你实现一个简单的 XML 的解析器,应该怎么实现?
225+
- 如果让你检查 XML 标签是否闭合如何检查, 更进一步如果要你实现一个简单的 XML 的解析器,应该怎么实现?

0 commit comments

Comments
 (0)