1 Star 0 Fork 1

dllearn/geektime-algorithm-learn

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
f.go 619 Bytes
一键复制 编辑 原始数据 按行查看 历史
dryyun 提交于 2022-04-15 00:32 +08:00 . 0020
package _020_valid_parentheses
// 20. 有效的括号
// https://leetcode-cn.com/problems/valid-parentheses/
func isValid(s string) bool {
stack := make([]int32, 0, len(s))
// ( = 40
// ) = 41
// { = 123
// } = 125
// [ = 91
// ] = 93
for _, a := range s {
if a == 40 || a == 123 || a == 91 {
stack = append(stack, a)
} else if a == 41 || a == 125 || a == 93 {
l := len(stack)
if l == 0 {
return false
}
pop := stack[l-1]
if (a == 41 && pop != 40) || (a == 125 && pop != 123) || (a == 93 && pop != 91) {
return false
}
stack = stack[:l-1]
}
}
return len(stack) == 0
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/dllearn/geektime-algorithm-learn.git
git@gitee.com:dllearn/geektime-algorithm-learn.git
dllearn
geektime-algorithm-learn
geektime-algorithm-learn
55fa3f222b36

搜索帮助