Given a non-negative integer num, Return its encoding string.
The encoding is done by converting the integer to a string using a secret function that you should deduce from the following table:
Example 1:
1 | Input: num = 23 |
Example 2:
1 | Input: num = 107 |
Constraints:
1 | 0 <= num <= 10^9 |
Approach
1 | Follow the rule of encoding, we can find the lenght of the output increase by one bit after increase the number by the power of 2. So when the number is 23, we can calculate the lenght of the result by the formula 1 (+1bit) + 2 (+1bit) + 4 (+1bit) + 8 (+1bit). |
Algorithm
1 | 1. Find the largest sum of power of 2 which is smaller than num. |
Implementation
1 | char * encode(int num){ |