Skip to content

Commit 7c73299

Browse files
committed
Optimize 1-bit and 2-bit Characters.
1 parent e0238c7 commit 7c73299

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

Easy/717.1-bit and 2-bit Characters.playground/Contents.swift

+7-6
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,16 @@
2626
*/
2727
class Solution {
2828
func isOneBitCharacter(_ bits: [Int]) -> Bool {
29+
if bits.last != 0 { return false }
2930
var result = [Int]()
30-
for (i, c) in bits.enumerated() {
31-
if c == 0 && i != bits.count - 1 {
32-
result.removeAll()
33-
} else {
34-
result.append(c)
31+
var index = bits.count - 1
32+
while index >= 0 {
33+
if index < bits.count - 1 && bits[index] == 0 {
34+
break
3535
}
36+
result.append(bits[index])
37+
index -= 1
3638
}
37-
if result.last != 0 { return false }
3839
return (result.count - 1) % 2 == 0
3940
}
4041
}

0 commit comments

Comments
 (0)