You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> 事实上,这类问题还可以进一步扩展,我们可以去解析类似 HTML 等标记语法, 比如 <p></p> <body></body>
66
68
67
-
## 关键点解析
69
+
###关键点解析
68
70
69
71
1. 栈的基本特点和操作
70
72
2. 如果你用的是 JS 没有现成的栈,可以用数组来模拟
71
73
入: push 出:pop
72
74
73
75
> 入: push 出 shift 就是队列
74
76
75
-
## 代码
77
+
###代码
76
78
77
-
- 语言支持:JS,Python
79
+
代码支持:JS,Python
78
80
79
81
Javascript Code:
80
82
@@ -112,7 +114,7 @@ var isValid = function (s) {
112
114
113
115
Python Code:
114
116
115
-
```
117
+
```py
116
118
classSolution:
117
119
defisValid(self,s):
118
120
stack = []
@@ -136,6 +138,88 @@ Python Code:
136
138
returnlen(stack) ==0
137
139
```
138
140
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>)
0 commit comments