1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| func isValid(s string) bool { var tmp []string
arr := strings.Split(s, "") for _, x := range arr { c := x if c == "(" || c == "[" || c == "{" { tmp = append(tmp, c) } else { if len(tmp) == 0 { return false }
charTop := tmp[len(tmp)-1] tmp = append(tmp[:len(tmp)-1], tmp[len(tmp):]...) if c == ")" && charTop != "(" { return false } if c == "]" && charTop != "[" { return false }
if c == "}" && charTop != "{" { return false } } }
if len(tmp) != 0 { return false }
return true }
func isValid(s string) bool { var ( tmp []string True bool = true ) validMap := map[string]string{ ")": "(", "}": "{", "]": "[", }
sArray := strings.Split(s, "")
for _, x := range sArray { if _, ok := validMap[x]; !ok { tmp = append(tmp, x) continue } if len(tmp) != 0 { v := tmp[len(tmp)-1] tmp = append(tmp[:len(tmp)-1], tmp[len(tmp):]...) if v != validMap[x] { True = false break } } else { True = false break } } if len(tmp) != 0 { True = false }
return True }
|