这道题目很巧妙,似乎是有些过于巧妙了,属于我未知领域的类型,把原作者的解决方案放这里。(看大家都在给差评,我也顺手给个差评吧~~)

参考:https://leetcode.com/problems/convert-to-base-2/discuss/265507/JavaC%2B%2BPython-2-lines-Exactly-Same-as-Base-2

 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]))

 

posted on 2019-04-01 09:34  Sempron2800+  阅读(140)  评论(0编辑  收藏  举报