Binary Representation to Number
EasyP3Coding
Given a string bits made only of the characters 0 and 1, return the integer it represents in base 2. The string may have leading zeros.
Do not use parseInt(bits, 2) or int(bits, 2).
Example 1
- Input
- bits = "1010"
- Output
- 10
Example 2
- Input
- bits = "0"
- Output
- 0
Example 3
- Input
- bits = "1"
- Output
- 1
Constraints
- 1 <= bits.length <= 31
- bits[i] is "0" or "1"
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 10. ⌘Enter runs, ⌘⇧Enter submits.