其他分享
首页 > 其他分享> > leetcode--20.有效的括号

leetcode--20.有效的括号

作者:互联网

func isValid(s string) bool {
	leftMap := map[string]string{"(": "", "[": "", "{": ""}
	r2lMap := map[string]string{")": "(", "]": "[", "}": "{"}
	sQ := make([]string, 0,len(s))
	for _, cB := range s {
		cS := string(cB)
		if _, ok := leftMap[cS]; ok {
			sQ = append(sQ, cS)
		} else {
			trip := r2lMap[cS]
			if len(sQ) < 1 {
				return false
			}
			if sQ[len(sQ)-1] == trip {
				if len(sQ) > 1 {
					sQ = sQ[:len(sQ)-1]
				} else if len(sQ) ==1{
					sQ = []string{}
				}else {
					return false
				}
			} else {
				return false
			}
		}
	}
	if len(sQ) > 0 {
		return false
	}
	return true
}

标签:20,string,--,sQ,len,cS,return,false,leetcode
来源: https://blog.csdn.net/hotpotbo/article/details/122792180