add new solutions
This commit is contained in:
parent
ccb8b5673b
commit
58527849b2
6 changed files with 202 additions and 0 deletions
32
solutions/0/q20/solution.go
Normal file
32
solutions/0/q20/solution.go
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
package q20
|
||||
|
||||
func isValid(s string) bool {
|
||||
stack := make([]byte, 0, len(s)/2)
|
||||
|
||||
for i := range len(s) {
|
||||
switch s[i] {
|
||||
case '(', '[', '{':
|
||||
stack = append(stack, s[i])
|
||||
case ')':
|
||||
if len(stack) == 0 || stack[len(stack)-1] != '(' {
|
||||
return false
|
||||
}
|
||||
stack = stack[:len(stack)-1]
|
||||
|
||||
case ']':
|
||||
if len(stack) == 0 || stack[len(stack)-1] != '[' {
|
||||
return false
|
||||
}
|
||||
stack = stack[:len(stack)-1]
|
||||
|
||||
case '}':
|
||||
if len(stack) == 0 || stack[len(stack)-1] != '{' {
|
||||
return false
|
||||
}
|
||||
stack = stack[:len(stack)-1]
|
||||
}
|
||||
}
|
||||
return len(stack) == 0
|
||||
}
|
||||
|
||||
var _ = isValid
|
||||
Loading…
Add table
Add a link
Reference in a new issue