commit c178e985e197da9b8435fa5982a887301952044f
parent 352293fa5bbbb722eea3825fced7fd00234a33e8
Author: Jack Sangdahl <jack@pngu.org>
Date: Mon, 17 Nov 2025 18:30:51 -0700
717. 1-bit and 2-bit characters
Diffstat:
2 files changed, 48 insertions(+), 0 deletions(-)
diff --git a/1bit_2bit_chars.c b/1bit_2bit_chars.c
@@ -0,0 +1,22 @@
+// 717. 1-bit and 2-bit Characters
+#include "leetcode.h"
+
+/*
+ * we have two special characters: the first character is represented by one bit
+ * 0, and the second character can be represented by two bits (10 or 11). given
+ * a binary array 'bits' that ends with 0, return true if the last character
+ * must be a one-bit character.
+ */
+
+bool isOneBitCharacter(int *bits, int bitsSize) {
+ int i = bitsSize - 2;
+ while (i >= 0 && bits[i] > 0)
+ i--;
+ return !((bitsSize - i) % 2);
+}
+
+int main() {
+ int b1[] = {1, 0, 0}, b2[] = {1, 1, 1, 0};
+ printf("%d\n", isOneBitCharacter(b1, ARRAY_SIZE(b1))); // expect: 1
+ printf("%d\n", isOneBitCharacter(b2, ARRAY_SIZE(b2))); // expect: 0
+}
diff --git a/1bit_2bit_chars.py b/1bit_2bit_chars.py
@@ -0,0 +1,26 @@
+# 717. 1-bit and 2-bit Characters
+
+"""
+we have two special characters: the first character is represented by one bit
+0, and the second character can be represented by two bits (10 or 11). given
+a binary array 'bits' that ends with 0, return true if the last character
+must be a one-bit character.
+"""
+
+
+class Solution(object):
+ def isOneBitCharacter(self, bits):
+ """
+ :type bits: List[int]
+ :rtype: bool
+ """
+ parity = bits.pop()
+ while bits and bits.pop():
+ parity ^= 1
+ return parity == 0
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.isOneBitCharacter(bits=[1, 0, 0]))
+ print(obj.isOneBitCharacter(bits=[1, 1, 1, 0]))