Binary to Decimal
Given a canonical binary string binary, return the non-negative decimal integer it represents. A canonical binary string contains only 0 and 1 and has no…
Given a canonical binary string `binary`, return the non-negative decimal integer it represents. A canonical binary string contains only `0` and `1` and has no leading zeroes, except that zero itself is represented by the single string `"0"`.
- `1 <= binary.length <= 30` - `binary` contains only `0` and `1`. - `binary` is exactly `"0"` or begins with `1`. - The represented value fits in a signed 32-bit integer.
Example 1: input binary = "101010"; output 42. The set bits contribute 32 + 8 + 2, so the decimal value is 42.
Example 2: input binary = "10000"; output 16. The only set bit has positional weight 16.
Example 3: input binary = "0"; output 0. The canonical binary representation of zero maps to decimal zero.
Category: Math. Topic: math. Difficulty: Easy. Patterns: math.