Skip to content

Instantly share code, notes, and snippets.

@luojiyin1987
Created July 19, 2018 14:59
Show Gist options
  • Save luojiyin1987/2d24996209bbf792f8831d66e849e2a7 to your computer and use it in GitHub Desktop.
Save luojiyin1987/2d24996209bbf792f8831d66e849e2a7 to your computer and use it in GitHub Desktop.
#golang #leetcode
func isValid(s string) bool {
stack := []rune{}
for _, b := range s {
switch b {
case '(', '[', '{':
stack = append([]rune{b}, stack...)
default:
if len(stack) > 0 {
c := stack[0]; stack = stack[1:]
switch c {
case '(':
if b != ')' { return false }
case '[':
if b != ']' { return false }
case '{':
if b != '}' { return false }
}
} else {
return false
}
}
}
return len(stack) == 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment