-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBase7.java
More file actions
31 lines (31 loc) · 712 Bytes
/
Base7.java
File metadata and controls
31 lines (31 loc) · 712 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
package easy;
/**
* ClassName: Base7.java
* Author: chenyiAlone
* Create Time: 2019/9/23 20:39
* Description: No.504 Base 7
*
* Given an integer, return its base 7 string representation.
*
* 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 String convertToBase7(int num) {
boolean flag = num < 0;
num = Math.abs(num);
StringBuilder ret = new StringBuilder(num == 0 ? "0": "");
while (num != 0) {
ret.append(num % 7);
num /= 7;
}
if (flag) ret.append('-');
return ret.reverse().toString();
}
}