Leetcode Note: Go - 1 Bit and 2 Bit Characters

1-bit and 2-bit Characters - LeetCode
https://leetcode.com/problems/1-bit-and-2-bit-characters/

  • Go 言語で取り組んだメモ

所感

  • int 配列 bits が渡される
  • 特殊文字
    • 1 bit: 0
    • 2 bits: 10, 11
  • 0 で終わるまたは 1 bit で終わる文字だった場合 true を return する

回答

0 ms, faster than 100.00% of Go online submissions - LeetCode Discuss
https://leetcode.com/problems/1-bit-and-2-bit-characters/discuss/1050762/0-ms-faster-than-100.00-of-Go-online-submissions

func isOneBitCharacter(bits []int) bool {
    l := len(bits)
    
    if l == 1 && bits[0] == 0 {
        return true
    } else if l == 2 && bits[0] == 1 {
        return false
    }
    
    for i := 1; i < l-1; i ++ {
        if bits[i-1] == 1 {
            bits[i], bits[i-1] = -1, -1
            i++
        }
    }
    
    if bits[l-2] == -1 {
        return true
    }
    
    return bits[l-2] != 1
}