Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit aef8f1a

Browse files
authored
Update Base 7.java
1 parent 152c7a2 commit aef8f1a

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

Easy/Base 7.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
class Solution {
2-
public String convertToBase7(int num) {
3-
if (num == 0) return "0";
4-
int temp = Math.abs(num);
5-
StringBuilder sb = new StringBuilder("");
6-
while (temp > 0) {
7-
sb.append(String.valueOf(temp%7));
8-
temp /= 7;
9-
}
10-
return num < 0 ? "-" + sb.reverse().toString() : sb.reverse().toString();
2+
public String convertToBase7(int num) {
3+
StringBuilder sb = new StringBuilder();
4+
char sign = num < 0 ? '-' : ' ';
5+
num = Math.abs(num);
6+
while (num > 0) {
7+
sb.append(num % 7);
8+
num /= 7;
119
}
10+
String representation = sb.length() == 0 ? "0" : sb.reverse().toString();
11+
if (sign == '-') {
12+
return sign + representation;
13+
}
14+
return representation;
15+
}
1216
}

0 commit comments

Comments
 (0)