Description
Given an integer, return its base 7 string representation.
Example 1:
Input: 100
Output: “202”
Example 2:
Input: -7
Output: “-10”
Note: The input will be in range of [-1e7, 1e7].
解法:
题目就是将十进制转变为七进制。
七进制各个位分别代表:1,7,49,343,……..
比如对于100,
右边第一位:100 % 7
右边第二位:100 / 7 % 7
右边第三位:100 / 7 / 7 % 7
所以得到七进制为202。
代码实现:
public String convertToBase7(int num) {
if(num == 0) return "0";
if(num < 0) return "-" + convertToBase7(-num);
if(num < 7) return String.valueOf(num);
return convertToBase7(num / 7) + num % 7;
}