Given an integer, return its base 7 string representation.
把一个整数转换成7进制的字符串
解题思路:直接toString(7)或除7取余
/**
* @param {number} num
* @return {string}
*/
var convertToBase7 = function(num) {
return num.toString(7)
};
除7取余
/**
* @param {number} num
* @return {string}
*/
var convertToBase7 = function(num) {
if (num === 0) {
return '0'
}
var re = []
var remainder = Math.abs(num)
while(remainder >= 1) {
re.unshift(remainder % 7)
remainder = parseInt(remainder / 7)
}
return num > 0 ? re.join('') : '-' + re.join('')
};