0
点赞
收藏
分享

微信扫一扫

LeetCode题解(1521):找到最接近目标值的函数值(Python)


题目:​​原题链接​​(困难)

标签:位运算、二分查找、线段树

解法

时间复杂度

空间复杂度

执行用时

Ans 1 (Python)

548ms (95.12%)

Ans 2 (Python)

Ans 3 (Python)

解法一(暴力解法):

class Solution:
def closestToTarget(self, arr: List[int], target: int) -> int:
ans = min(abs(num - target) for num in arr)

old_box = [arr]
while old_box:
new_box = []
for old_lst in old_box:
new_lst = []
for i in range(len(old_lst) - 1):
num = old_lst[i] & old_lst[i + 1]
ans = min(ans, abs(num - target))
if not new_lst or num != new_lst[-1]:
new_lst.append(num)
if new_lst:
new_box.append(new_lst)
old_box = new_box

return


举报

相关推荐

相加等于目标值的两个数

0 条评论