730. Convert Binary to Decimal Using Recursion

EasyRecursionRecursion

Given a binary string s (containing only characters '0' and '1', optionally prefixed with '-' to indicate a negative number), return the integer value it represents. The most significant bit is on the left. Implement the conversion recursively. You may assume the result fits in a 64-bit signed integer.

Input: A non-empty binary string s enclosed in double quotes.

Output: Return an integer equal to the decimal value of s.

Examples

Example 1
Input: "1010"
Output: 10
Explanation: 1*8 + 0*4 + 1*2 + 0*1 = 10.
Example 2
Input: "0"
Output: 0
Explanation: Zero.
Example 3
Input: "-101"
Output: -5
Explanation: Binary 101 is 5; the minus sign makes it -5.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →