Skip to content

Commit a61cbbe

Browse files
committed
solve problem Base 7
1 parent e886484 commit a61cbbe

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ All solutions will be accepted!
129129
|455|[assign cookies](https://leetcode-cn.com/problems/assign-cookies/description/)|[java/py/js](./algorithms/AssignCookies)|Easy|
130130
|415|[Add Strings](https://leetcode-cn.com/problems/add-strings/description/)|[java/py/js](./algorithms/AddStrings)|Easy|
131131
|67|[Add Binary](https://leetcode-cn.com/problems/add-binary/description/)|[java/py/js](./algorithms/AddBinary)|Easy|
132+
|504|[Base 7](https://leetcode-cn.com/problems/base-7/description/)|[java/py/js](./algorithms/Base7)|Easy|
132133

133134
# Database
134135
|#|Title|Solution|Difficulty|

algorithms/Base7/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Base 7
2+
This problem is easy to solve

algorithms/Base7/Solution.java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public String convertToBase7(int num) {
3+
String res = "";
4+
boolean isNegative = false;
5+
6+
if (num == 0) {
7+
res = "0";
8+
} else {
9+
if (num < 0) {
10+
num = -num;
11+
isNegative = true;
12+
}
13+
14+
while (num > 0) {
15+
res = String.valueOf(num % 7) + res;
16+
num /= 7;
17+
}
18+
}
19+
20+
return isNegative ? "-" + res : res;
21+
}
22+
}

algorithms/Base7/solution.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {number} num
3+
* @return {string}
4+
*/
5+
var convertToBase7 = function(num) {
6+
let res = '',
7+
isNegative = false
8+
9+
if (num === 0) {
10+
res = '0'
11+
} else {
12+
if (num < 0) {
13+
num = -num
14+
isNegative = true
15+
}
16+
17+
while (num > 0) {
18+
res = String(num % 7) + res
19+
num = parseInt(num / 7)
20+
}
21+
}
22+
23+
return isNegative ? '-' + res : res
24+
};

algorithms/Base7/solution.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution(object):
2+
def convertToBase7(self, num):
3+
"""
4+
:type num: int
5+
:rtype: str
6+
"""
7+
res = ''
8+
is_negative = False
9+
10+
if num == 0:
11+
res = '0'
12+
if num < 0:
13+
num = -num
14+
is_negative = True
15+
while num > 0:
16+
res = str(num % 7) + res
17+
num = num / 7
18+
return '-' + res if is_negative else res

0 commit comments

Comments
 (0)