Skip to content

Commit 6e09284

Browse files
committedMar 7, 2022
feat: add solutions to lc problem: No.0504
No.0504.Base 7
1 parent 38e747a commit 6e09284

File tree

5 files changed

+165
-37
lines changed

5 files changed

+165
-37
lines changed
 

‎solution/0500-0599/0504.Base 7/README.md

+63-7
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,31 @@
66

77
<!-- 这里写题目描述 -->
88

9-
<p>给定一个整数,将其转化为7进制,并以字符串形式输出。</p>
9+
<p>给定一个整数 <code>num</code>,将其转化为 <strong>7 进制</strong>,并以字符串形式输出。</p>
10+
11+
<p>&nbsp;</p>
1012

1113
<p><strong>示例 1:</strong></p>
1214

1315
<pre>
14-
<strong>输入:</strong> 100
15-
<strong>输出:</strong> &quot;202&quot;
16+
<strong>输入:</strong> num = 100
17+
<strong>输出:</strong> "202"
1618
</pre>
1719

1820
<p><strong>示例 2:</strong></p>
1921

2022
<pre>
21-
<strong>输入:</strong> -7
22-
<strong>输出:</strong> &quot;-10&quot;
23+
<strong>输入:</strong> num = -7
24+
<strong>输出:</strong> "-10"
2325
</pre>
2426

25-
<p><strong>注意:</strong> 输入范围是&nbsp;[-1e7, 1e7] 。</p>
27+
<p>&nbsp;</p>
28+
29+
<p><strong>提示:</strong></p>
30+
31+
<ul>
32+
<li><code>-10<sup>7</sup>&nbsp;&lt;= num &lt;= 10<sup>7</sup></code></li>
33+
</ul>
2634

2735
## 解法
2836

@@ -35,7 +43,17 @@
3543
<!-- 这里可写当前语言的特殊实现逻辑 -->
3644

3745
```python
38-
46+
class Solution:
47+
def convertToBase7(self, num: int) -> str:
48+
if num == 0:
49+
return '0'
50+
if num < 0:
51+
return '-' + self.convertToBase7(-num)
52+
ans = []
53+
while num:
54+
ans.append(str(num % 7))
55+
num //= 7
56+
return ''.join(ans[::-1])
3957
```
4058

4159
### **Java**
@@ -108,6 +126,44 @@ impl Solution {
108126
}
109127
```
110128

129+
### **C++**
130+
131+
```cpp
132+
class Solution {
133+
public:
134+
string convertToBase7(int num) {
135+
if (num == 0) return "0";
136+
if (num < 0) return "-" + convertToBase7(-num);
137+
string ans = "";
138+
while (num)
139+
{
140+
ans = to_string(num % 7) + ans;
141+
num /= 7;
142+
}
143+
return ans;
144+
}
145+
};
146+
```
147+
148+
### **Go**
149+
150+
```go
151+
func convertToBase7(num int) string {
152+
if num == 0 {
153+
return "0"
154+
}
155+
if num < 0 {
156+
return "-" + convertToBase7(-num)
157+
}
158+
ans := []byte{}
159+
for num != 0 {
160+
ans = append([]byte{'0' + byte(num%7)}, ans...)
161+
num /= 7
162+
}
163+
return string(ans)
164+
}
165+
```
166+
111167
### **...**
112168

113169
```

‎solution/0500-0599/0504.Base 7/README_EN.md

+63-30
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,22 @@
44

55
## Description
66

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>&nbsp;</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"
1716
</pre>
17+
<p>&nbsp;</p>
18+
<p><strong>Constraints:</strong></p>
1819

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> &lt;= num &lt;= 10<sup>7</sup></code></li>
22+
</ul>
3823

3924
## Solutions
4025

@@ -43,7 +28,17 @@ The input will be in range of [-1e7, 1e7].
4328
### **Python3**
4429

4530
```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])
4742
```
4843

4944
### **Java**
@@ -114,6 +109,44 @@ impl Solution {
114109
}
115110
```
116111

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+
117150
### **...**
118151

119152
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
string convertToBase7(int num) {
4+
if (num == 0) return "0";
5+
if (num < 0) return "-" + convertToBase7(-num);
6+
string ans = "";
7+
while (num)
8+
{
9+
ans = to_string(num % 7) + ans;
10+
num /= 7;
11+
}
12+
return ans;
13+
}
14+
};
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func convertToBase7(num int) string {
2+
if num == 0 {
3+
return "0"
4+
}
5+
if num < 0 {
6+
return "-" + convertToBase7(-num)
7+
}
8+
ans := []byte{}
9+
for num != 0 {
10+
ans = append([]byte{'0' + byte(num%7)}, ans...)
11+
num /= 7
12+
}
13+
return string(ans)
14+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def convertToBase7(self, num: int) -> str:
3+
if num == 0:
4+
return '0'
5+
if num < 0:
6+
return '-' + self.convertToBase7(-num)
7+
ans = []
8+
while num:
9+
ans.append(str(num % 7))
10+
num //= 7
11+
return ''.join(ans[::-1])

0 commit comments

Comments
 (0)