有人可以给出最简单的解决方案,将整数转换为表示其相关二进制数字的整数数组。
Input => Output
1 => [1]
2 => [2]
3 => [2,1]
4 => [4]
5 => [4,1]
6 => [4,2]
One way is :
Step 1 : 9.to_s(2) #=> "1001"
Step 2 : loop with the count of digit
use / and %
based on loop index, multiply with 2
store in a array
还有其他直接或更好的解决方案吗?