4
4
5
5
## Description
6
6
7
- <p >Given an integer, return its base 7 string representation.</p >
8
-
9
- <p ><b >Example 1:</b ><br />
10
-
11
- <pre >
12
-
13
- <b >Input:</b > 100
14
-
15
- <b >Output:</b > "202"
16
-
7
+ <p >Given an integer <code >num</code >, return <em >a string of its <strong >base 7</strong > representation</em >.</p >
8
+
9
+ <p >  ; </p >
10
+ <p ><strong >Example 1:</strong ></p >
11
+ <pre ><strong >Input:</strong > num = 100
12
+ <strong >Output:</strong > "202"
13
+ </pre ><p ><strong >Example 2:</strong ></p >
14
+ <pre ><strong >Input:</strong > num = -7
15
+ <strong >Output:</strong > "-10"
17
16
</pre >
17
+ <p >  ; </p >
18
+ <p ><strong >Constraints:</strong ></p >
18
19
19
- </p >
20
-
21
- <p ><b >Example 2:</b ><br />
22
-
23
- <pre >
24
-
25
- <b >Input:</b > -7
26
-
27
- <b >Output:</b > "-10"
28
-
29
- </pre >
30
-
31
- </p >
32
-
33
- <p ><b >Note:</b >
34
-
35
- The input will be in range of [ -1e7, 1e7] .
36
-
37
- </p >
20
+ <ul >
21
+ <li><code>-10<sup>7</sup> <= num <= 10<sup>7</sup></code></li>
22
+ </ul >
38
23
39
24
## Solutions
40
25
@@ -43,7 +28,17 @@ The input will be in range of [-1e7, 1e7].
43
28
### ** Python3**
44
29
45
30
``` python
46
-
31
+ class Solution :
32
+ def convertToBase7 (self , num : int ) -> str :
33
+ if num == 0 :
34
+ return ' 0'
35
+ if num < 0 :
36
+ return ' -' + self .convertToBase7(- num)
37
+ ans = []
38
+ while num:
39
+ ans.append(str (num % 7 ))
40
+ num //= 7
41
+ return ' ' .join(ans[::- 1 ])
47
42
```
48
43
49
44
### ** Java**
@@ -114,6 +109,44 @@ impl Solution {
114
109
}
115
110
```
116
111
112
+ ### ** C++**
113
+
114
+ ``` cpp
115
+ class Solution {
116
+ public:
117
+ string convertToBase7(int num) {
118
+ if (num == 0) return "0";
119
+ if (num < 0) return "-" + convertToBase7(-num);
120
+ string ans = "";
121
+ while (num)
122
+ {
123
+ ans = to_string(num % 7) + ans;
124
+ num /= 7;
125
+ }
126
+ return ans;
127
+ }
128
+ };
129
+ ```
130
+
131
+ ### **Go**
132
+
133
+ ```go
134
+ func convertToBase7(num int) string {
135
+ if num == 0 {
136
+ return "0"
137
+ }
138
+ if num < 0 {
139
+ return "-" + convertToBase7(-num)
140
+ }
141
+ ans := []byte{}
142
+ for num != 0 {
143
+ ans = append([]byte{'0' + byte(num%7)}, ans...)
144
+ num /= 7
145
+ }
146
+ return string(ans)
147
+ }
148
+ ```
149
+
117
150
### ** ...**
118
151
119
152
```
0 commit comments