leetcode1017

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

参考: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]))

猜你喜欢

转载自www.cnblogs.com/asenyang/p/10634625.html