leetcode

Description:

leetcode-717

Submission:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class :

# 三种情况:以1为结尾,一定为False;以0为结尾,且倒数第二位是0,一定为True;以0为结尾,且倒数第二位是1,这时考虑直到倒数第二个0出现前,连续的1有多少个,有奇数个,则最后的编码是‘10’,有偶数个,则最后的编码是‘0’。
def isOneBitCharacter(self, bits: List[int]) -> bool:
if (bits[-1] == 1):
return False
else:
n = len(bits)
if (n == 1):
return True
elif (bits[-2] == 0):
return True
else:
cnt = 0
for i in range(n-2, -1, -1):
if (bits[i] == 1):
cnt += 1
else:
break
return not bool(cnt%2)

Acceptance:

ac