Encode Number Problem
Description
LeetCode Problem 1256.
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:
Input: num = 23
Output: "1000"
Example 2:
Input: num = 107
Output: "101100"
Constraints:
0 <= num <= 10^9
Solution
Approach: Binary of num + 1
Reading the table, the codes are every binary string in order of length, and within one length in increasing order: "", "0", "1", "00", "01", "10", "11", "000", ...
Now write num + 1 in binary next to each code: 1 = 1, 2 = 10, 3 = 11, 4 = 100, 5 = 101, ... Every number of k + 1 bits is a 1 followed by some k-bit string, and these appear in the same order as the codes of length k. So the code of num is the binary form of num + 1 with its leading 1 removed.
Example: 23 + 1 = 24 = 11000, and removing the leading 1 gives "1000". The edge case num = 0 gives 1, so the code is the empty string.
Complexity: O(log num) time and space.
class Solution:
def encode(self, num: int) -> str:
# The codes list every binary string by length, then in order: num + 1 in binary minus its leading 1.
return bin(num + 1)[3:] # bin() starts with "0b1"