We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 152c7a2 commit aef8f1aCopy full SHA for aef8f1a
Easy/Base 7.java
@@ -1,12 +1,16 @@
1
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();
+ public String convertToBase7(int num) {
+ StringBuilder sb = new StringBuilder();
+ char sign = num < 0 ? '-' : ' ';
+ num = Math.abs(num);
+ while (num > 0) {
+ sb.append(num % 7);
+ num /= 7;
11
}
+ String representation = sb.length() == 0 ? "0" : sb.reverse().toString();
+ if (sign == '-') {
12
+ return sign + representation;
13
+ }
14
+ return representation;
15
16
0 commit comments