这道题目很巧妙,似乎是有些过于巧妙了,属于我未知领域的类型,把原作者的解决方案放这里。(看大家都在给差评,我也顺手给个差评吧~~)
1 class Solution(object): 2 def base2(self, x): 3 res = [] 4 while x: 5 res.append(x & 1) 6 x = x >> 1 7 return "".join(map(str, res[::-1] or [0])) 8 9 def baseNeg2(self, x): 10 res = [] 11 while x: 12 res.append(x & 1) 13 x = -(x >> 1) 14 return "".join(map(str, res[::-1] or [0]))