-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBase_504.java
More file actions
30 lines (27 loc) · 751 Bytes
/
Base_504.java
File metadata and controls
30 lines (27 loc) · 751 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
30
public class Base_504 {
// Most voted solution using recursion
public String convertToBase7(int num) {
// Good submission
// return Integer.toString(num, 7);
if (num < 0)
return '-' + convertToBase7(-num);
if (num < 7)
return num + "";
return convertToBase7(num / 7) + num % 7;
}
// My first solution using iteration
/*
public String convertToBase7(int num) {
if (num == 0)
return "0";
if (num < 0)
return "-" + convertToBase7(-num);
StringBuilder sb = new StringBuilder();
while (num > 0) {
sb.insert(0, num % 7);
num /= 7;
}
return sb.toString();
}
*/
}