-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1017.java
More file actions
59 lines (58 loc) · 1.23 KB
/
Copy path1017.java
File metadata and controls
59 lines (58 loc) · 1.23 KB
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
// 1017. Convert to Base -2
//
// Given an integer n, return a binary string representing its representation in base -2.
//
// Note that the returned string should not have leading zeros unless the string is "0".
//
//
//
// Example 1:
//
// Input: n = 2
// Output: "110"
// Explantion: (-2)2 + (-2)1 = 2
// Example 2:
//
// Input: n = 3
// Output: "111"
// Explantion: (-2)2 + (-2)1 + (-2)0 = 3
// Example 3:
//
// Input: n = 4
// Output: "100"
// Explantion: (-2)2 = 4
//
//
// Constraints:
//
// 0 <= n <= 109
//
//
// Runtime 1 ms Beats 57.30%
// Memory 40.4 MB Beats 28.9%
class Solution {
public String baseNeg2(int n) {
StringBuilder sb = new StringBuilder();
while (n != 0) {
if (n % 2 == 0) {
sb.append(0);
} else {
sb.append(1);
}
n = -(n >> 1);
}
return sb.length() >= 1 ? sb.reverse().toString(): "0";
}
}
// Runtime 0 ms Beats 100%
// Memory 40.5 MB Beats 28.9%
class Solution {
public String baseNeg2(int n) {
StringBuilder sb = new StringBuilder();
while (n != 0) {
sb.append(n & 1);
n = -(n >> 1);
}
return sb.length() >= 1 ? sb.reverse().toString(): "0";
}
}