-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathBase7
More file actions
29 lines (27 loc) · 685 Bytes
/
Base7
File metadata and controls
29 lines (27 loc) · 685 Bytes
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
26
27
28
29
//Given an integer, return its base 7 string representation.
class Solution {
public String convertToBase7(int num) {
StringBuilder str = new StringBuilder();
StringBuilder reverse = new StringBuilder();
if(num < 0)
{
reverse.append("-");
num *= -1;
}
if(num == 0)
{
str.append("0");
}
while(num != 0)
{
str.append(num%7);
num /= 7;
}
String temp = str.toString();
for(int i = temp.length() - 1; i >=0; i--)
{
reverse.append(temp.charAt(i));
}
return reverse.toString();
}
}