File tree Expand file tree Collapse file tree 1 file changed +25
-1
lines changed Expand file tree Collapse file tree 1 file changed +25
-1
lines changed Original file line number Diff line number Diff line change @@ -283,6 +283,31 @@ var isValid = function(s) {
283
283
};
284
284
```
285
285
286
+
287
+ Swift
288
+ ``` swift
289
+ func isValid (_ s : String ) -> Bool {
290
+ var stack = [String .Element ]()
291
+ for ch in s {
292
+ if ch == " (" {
293
+ stack.append (" )" )
294
+ } else if ch == " {" {
295
+ stack.append (" }" )
296
+ } else if ch == " [" {
297
+ stack.append (" ]" )
298
+ } else {
299
+ let top = stack.last
300
+ if ch == top {
301
+ stack.removeLast ()
302
+ } else {
303
+ return false
304
+ }
305
+ }
306
+ }
307
+ return stack.isEmpty
308
+ }
309
+ ```
310
+
286
311
C:
287
312
``` C
288
313
// 辅助函数:判断栈顶元素与输入的括号是否为一对。若不是,则返回False
@@ -324,6 +349,5 @@ bool isValid(char * s){
324
349
}
325
350
```
326
351
327
-
328
352
-----------------------
329
353
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
You can’t perform that action at this time.
0 commit comments