Content-Length: 306030 | pFad | http://github.com/xiaoyu2er/leetcode-js/commit/4f488e4c360bdef918986f049e4fdbf3635bf3e8
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 555086b commit 4f488e4Copy full SHA for 4f488e4
Easy_504_Base_7.js
@@ -0,0 +1,38 @@
1
+/**
2
+ *
3
+ * Given an integer, return its base 7 string representation.
4
+
5
+ Example 1:
6
+ Input: 100
7
+ Output: "202"
8
9
+ Example 2:
10
+ Input: -7
11
+ Output: "-10"
12
13
+ Note: The input will be in range of [-1e7, 1e7].
14
15
+ */
16
17
18
+ * @param {number} num
19
+ * @return {string}
20
21
+var convertToBase7 = function (num) {
22
23
+ if (num === 0) return '0';
24
+ var str = '';
25
+ var sign = num > 0;
26
+ num = Math.abs(num);
27
+ while (num) {
28
+ str = (num % 7) + str;
29
+ num = Math.floor(num / 7);
30
+ }
31
32
+ return sign ? str : '-' + str;
33
34
+};
35
36
+console.log(convertToBase7(0));
37
+console.log(convertToBase7(100));
38
+console.log(convertToBase7(-7));
Fetched URL: http://github.com/xiaoyu2er/leetcode-js/commit/4f488e4c360bdef918986f049e4fdbf3635bf3e8
Alternative Proxies:
Alternative Proxy
pFad Proxy
pFad v3 Proxy
pFad v4 Proxy
0 commit comments