Created
February 17, 2019 06:52
-
-
Save eklitzke/6aa6d016096079db592bc2d615a4f041 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func isValid(x int) bool { | |
s := 0 | |
for y := 1; y <= 0x2000; y <<= 1 { | |
if x&y == 0 { | |
// 0-bit means open paren, push the stack | |
s++ | |
} else { | |
// 1-bit means close paren, pop the stack | |
s-- | |
if s < 0 { | |
// detect invalid stack | |
return false | |
} | |
} | |
} | |
return s == 0 | |
} | |
func main() { | |
c := 0 | |
for i := 0; i <= 0x3fff; i++ { | |
if isValid(i) { | |
c++ | |
} | |
} | |
fmt.Println(c) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment