We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5c8dec2 commit e620421Copy full SHA for e620421
Python/convert-to-base-2.py
@@ -0,0 +1,35 @@
1
+# Time: O(logn)
2
+# Space: O(1)
3
+
4
+class Solution(object):
5
+ def baseNeg2(self, N):
6
+ """
7
+ :type N: int
8
+ :rtype: str
9
10
+ result = []
11
+ while N:
12
+ result.append(str(-N & 1)) # N % -2
13
+ N = -(N >> 1) # N //= -2
14
+ result.reverse()
15
+ return "".join(result) if result else "0"
16
17
18
19
20
+class Solution2(object):
21
22
23
24
25
26
+ BASE = -2
27
28
29
+ N, r = divmod(N, BASE)
30
+ if r < 0:
31
+ r -= BASE
32
+ N += 1
33
+ result.append(str(r))
34
35
0 commit comments