LeetCode: Base 7

LeetCode: Base 7

Given an integer, return its base 7 string representation.

Example 1:

1
2
Input: 100
Output: "202"

Example 2:

1
2
Input: -7
Output: "-10"

Note: The input will be in range of [-1e7, 1e7].

其实理解了进制转换之后,这题确实不难。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class Base7 {

public String convertToBase7(int num) {

StringBuilder sb = new StringBuilder();
boolean isNegative = false;
if (num < 0) {
isNegative = true;
num = Math.abs(num);
}

int a = num;

while (a >= 7) {
int b = a % 7;
a = a / 7;
sb.insert(0, b);
}
sb.insert(0, a);
if (isNegative) {
sb.insert(0, "-");
}
return sb.toString();
}
}