Leetcode 504 - Base 7

Note:

  • Just like how to convert decimal to binary numbers.
  • Divide num by 7 every time, and append remainder to the front till num becomes 0.

Question:

Given an integer num, return a string of its base 7 representation.

Example:

1
2
Input: num = 100
Output: "202"

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
* @param {number} num
* @return {string}
*/
var convertToBase7 = function(num) {
if (num === 0) return '0';
let res = '';
let isNegative = false;
if (num < 0) {
num = -num;
isNegative = true;
}
while (num !== 0) {
res = (num % 7) + res;
num = ~~(num / 7);
}
return (isNegative ? '-' : '') + res;
};