We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent e6db2ca commit a745b9bCopy full SHA for a745b9b
scripts/algorithms/B/Base 7/Base 7.java
@@ -1,20 +1,21 @@
1
+// Runtime: 1 ms (Top 62.45%) | Memory: 40.70 MB (Top 12.35%)
2
+
3
class Solution {
4
public String convertToBase7(int num) {
- if (num == 0) {
- return "0";
5
- }
6
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;
+ while(Math.abs(num) > 6) {
+ sb.append(num % 7);
+ num = num / 7;
15
}
16
if (num < 0) {
17
- sb.append("-");
+ sb.append(Math.abs(num)).append("-");
+ } else {
+ sb.append(num);
+ }
+ for (int i = 0; i < sb.length() - 1; i++) {
+ if (!Character.isDigit(sb.charAt(i))) {
+ sb.replace(i, i + 1, "");
18
19
20
return sb.reverse().toString();
21
0 commit comments