20. Valid Parentheses

20. Valid Parentheses,第1张

概述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

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所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1278005.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-09
下一篇2022-06-09

发表评论

登录后才能评论

评论列表(0条)

    保存