EasyNo. 0717
1-bit and 2-bit Characters — LeetCode 717 Python Solution
- Topics
- Array
- Reading time
- 2 min
- Source
- leetcode.com
The problem
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).
Example
- Input
- bits = [1,0,0]
- Output
- true
- Explanation
- The only way to decode it is two-bit character and one-bit character.
Python solution
class Solution:
def isOneBitCharacter(self, bits: List[int]) -> bool:
i, n = 0, len(bits)
while i < n - 1:
i += bits[i] + 1
return i == n - 1Complexity
- Time
- O(n)where n is the length of the array bits
- Space · auxiliary
- O(1)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 0057 | Insert Interval | Medium |
| 0228 | Summary Ranges | Easy |
| 0485 | Max Consecutive Ones | Easy |
| 0665 | Non-decreasing Array | Medium |
| 0674 | Longest Continuous Increasing Subsequence | Easy |
| 0699 | Falling Squares | Hard |
Frequently asked questions
- How hard is LeetCode 717. 1-bit and 2-bit Characters?
LeetCode 717. 1-bit and 2-bit Characters is rated Easy on LeetCode.
- What is the time complexity of LeetCode 717. 1-bit and 2-bit Characters?
The Python solution on this page runs in O(n), where n is the length of the array \textit{bits}.
- What is the space complexity of LeetCode 717. 1-bit and 2-bit Characters?
The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 717. 1-bit and 2-bit Characters cover?
LeetCode 717. 1-bit and 2-bit Characters is tagged Array on LeetCode.