Skip to content

Commit a745b9b

Browse files
committed
Runtime: 1 ms (Top 62.45%) | Memory: 40.70 MB (Top 12.35%)
1 parent e6db2ca commit a745b9b

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

scripts/algorithms/B/Base 7/Base 7.java

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1+
// Runtime: 1 ms (Top 62.45%) | Memory: 40.70 MB (Top 12.35%)
2+
13
class Solution {
24
public String convertToBase7(int num) {
3-
if (num == 0) {
4-
return "0";
5-
}
65
StringBuilder sb = new StringBuilder();
7-
int temp = num;
8-
if (temp < 0) {
9-
temp = -temp;
10-
}
11-
while (temp > 0) {
12-
int rem = temp % 7;
13-
sb.append(rem);
14-
temp = temp / 7;
6+
while(Math.abs(num) > 6) {
7+
sb.append(num % 7);
8+
num = num / 7;
159
}
1610
if (num < 0) {
17-
sb.append("-");
11+
sb.append(Math.abs(num)).append("-");
12+
} else {
13+
sb.append(num);
14+
}
15+
for (int i = 0; i < sb.length() - 1; i++) {
16+
if (!Character.isDigit(sb.charAt(i))) {
17+
sb.replace(i, i + 1, "");
18+
}
1819
}
1920
return sb.reverse().toString();
2021
}

0 commit comments

Comments
 (0)