forked from gouthampradhan/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBase7.java
More file actions
32 lines (29 loc) · 757 Bytes
/
Base7.java
File metadata and controls
32 lines (29 loc) · 757 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
31
32
/* (C) 2024 YourCompanyName */
package math;
import java.util.*;
/**
* Created by gouthamvidyapradhan on 01/08/2019 Given an integer, return its base 7 string
* representation.
*
* <p>Example 1: Input: 100 Output: "202" Example 2: Input: -7 Output: "-10" Note: The input will be
* in range of [-1e7, 1e7].
*/
public class Base7 {
public static void main(String[] args) {
//
}
public String convertToBase7(int num) {
Integer.toString(7, 7);
if (num == 0) return "0";
int q = Math.abs(num), r;
StringBuilder sb = new StringBuilder();
while (q != 0) {
r = q % 7;
sb.append(r);
q /= 7;
}
if (num < 0) {
return "-" + sb.reverse().toString();
} else return sb.reverse().toString();
}
}