-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
504.java
59 lines (58 loc) · 1.29 KB
/
504.java
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
__________________________________________________________________________________________________
sample 0 ms submission
/*
* @lc app=leetcode id=504 lang=java
*
* [504] Base 7
*
* https://leetcode.com/problems/base-7/description/
*
* algorithms
* Easy (45.07%)
* Likes: 157
* Dislikes: 121
* Total Accepted: 43.5K
* Total Submissions: 96.5K
* Testcase Example: '100'
*
* 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].
*
*/
class Solution {
public String convertToBase7(int num) {
return Integer.toString(num,7);
}
}
__________________________________________________________________________________________________
sample 35524 kb submission
class Solution {
public String convertToBase7(int num) {
StringBuilder sb = new StringBuilder();
int v = Math.abs(num);
sb.append(v%7);
int n = v/7;
while(n != 0) {
sb.insert(0,n % 7);
n = n / 7;
}
return num >= 0 ? sb.toString() : "-" + sb.toString();
}
}
__________________________________________________________________________________________________