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].
Solution:
思路:
Time Complexity: O(N) Space Complexity: O(N)
Solution Code:
class Solution {
public String convertTo7(int num) {
if (num < 0) {
return '-' + convertTo7(-num);
}
if (num < 7) {
return num + "";
}
return convertTo7(num / 7) + num % 7;
}
}
网友评论