add: 220220-CN [golang]

This commit is contained in:
Lam Haoyin 2022-02-20 12:38:52 +08:00
parent cdb4eb86a3
commit 705d3e255f
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
1 changed files with 27 additions and 0 deletions

27
golang/2202/220220-CN.go Normal file
View File

@ -0,0 +1,27 @@
package main
import "fmt"
func isOneBitCharacter(bits []int) bool {
var n = len(bits)
if n == 1 || bits[n-2] == 0 {
return true
}
// try applying codes from front to rear
vis := make([]bool, n)
vis[0] = true
for i := 0; i < n-2; i++ {
if !vis[i] {
continue
} else if bits[i] == 0 {
vis[1+i] = true
} else { // bits[i] == 1
vis[2+i] = true
}
}
return !vis[n-2]
}
func main() {
fmt.Println(isOneBitCharacter([]int{1, 1, 1, 0}))
}