https://leetcode-cn.com/problems/valid-parentheses/
func isValid(_ s: String) -> Bool {
if s.count <= 0 {return true}
var result = [Character]()
for (_, char) in s.enumerated() {
if char == "(" || char == "{" || char == "[" {
result.append(char)
} else {
if result.isEmpty {return false}
//获取result最后一个char
let lastChar = result.popLast()
if lastChar == "(" && char != ")"{return false}
if lastChar == "[" && char != "]"{return false}
if lastChar == "{" && char != "}"{return false}
}
}
return result.isEmpty
}
网友评论