Merge pull request #917 from qxuewei/master

添加 20. 有效的括号 Swift版本
This commit is contained in:
程序员Carl
2021-11-28 13:24:35 +08:00
committed by GitHub

View File

@ -283,6 +283,31 @@ var isValid = function(s) {
};
```
Swift
```swift
func isValid(_ s: String) -> Bool {
var stack = [String.Element]()
for ch in s {
if ch == "(" {
stack.append(")")
} else if ch == "{" {
stack.append("}")
} else if ch == "[" {
stack.append("]")
} else {
let top = stack.last
if ch == top {
stack.removeLast()
} else {
return false
}
}
}
return stack.isEmpty
}
```
C:
```C
//辅助函数判断栈顶元素与输入的括号是否为一对。若不是则返回False
@ -324,6 +349,5 @@ bool isValid(char * s){
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>