
Given a string containing just the characters ‘(‘,‘)’,‘{‘,‘}’,‘[’ and ‘]’,determine if the input string is valID.
The brackets must close in the correct order,“()” and “()[]{}” are all valID but “(]” and “([)]” are not.
括号匹配问题
func isValID(s string) bool { stack := []byte{} for i := 0; i < len(s); i++ { switch s[i] { case '(','[','{': stack = append(stack,s[i]) case ')': if len(stack) > 0 && stack[len(stack)-1] == '(' { stack = stack[:len(stack)-1] } else { return false } case ']': if len(stack) > 0 && stack[len(stack)-1] == '[' { stack = stack[:len(stack)-1] } else { return false } case '}': if len(stack) > 0 && stack[len(stack)-1] == '{' { stack = stack[:len(stack)-1] } else { return false } } } return (len(stack) == 0)}总结
以上是内存溢出为你收集整理的20. Valid Parentheses全部内容,希望文章能够帮你解决20. Valid Parentheses所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)