class tree():
def bulid_tree(self, ls):
self.ls = ls
self.root = {}
for num in ls:
cur = self.root
for i in range(14, -1, -1):
val = (num>>i)&1
cur.setdefault(val, {})
cur = cur[val]
def get_max(self):
max_val = 0
for num in self.ls:
cur = self.root
_val = 0
for i in range(14, -1, -1):
val = (num>>i)&1
if val^1 in cur:
_val += (1<<i)
cur = cur[val^1]
else:
cur = cur[val]
max_val = max(max_val, _val)
return max_val
t = tree()
t.bulid_tree([3, 10, 5, 25, 2, 8])
print(t.get_max())
leetcode 421
最新推荐文章于 2020-09-14 20:33:20 发布