mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
添加 20. 有效的括号 Swift版本
This commit is contained in:
@ -283,6 +283,29 @@ 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
|
||||
}
|
||||
```
|
||||
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
Reference in New Issue
Block a user