forked from keep-practicing/leetcode-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1bitand2bitc.go
43 lines (36 loc) · 818 Bytes
/
1bitand2bitc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
717. 1-bit and 2-bit Characters
https://leetcode.com/problems/1-bit-and-2-bit-characters/
We have two special characters.
The first character can be represented by one bit 0.
The second character can be represented by two bits (10 or 11).
Now given a string represented by several bits.
Return whether the last character must be a one-bit character or not.
The given string will always end with a zero.
Note:
1 <= len(bits) <= 1000.
bits[i] is always 0 or 1.
*/
// time: 2018-12-28
package onebitandtwobitcharacters
// time complexity: O(n)
// space complexity: O(1)
func isOneBitCharacter(bits []int) bool {
n := len(bits)
if 1 == n {
return true
}
cur := 0
flag := false
for cur < n {
if 0 == bits[cur] {
cur++
} else {
cur += 2
}
if cur == n-1 {
flag = true
}
}
return flag
}