我正在处理Leetcode问题“ Number of Boomerangs”。
我编写的python代码是
def numberOfBoomerangs(self, points):
"""
:type points: List[List[int]]
:rtype: int
"""
import numpy as np
count = 0
no_of_points = len(points)
points = np.array(points)
for ind_c in range(no_of_points):
dis_array = np.zeros((no_of_points))
for ind_n in range(no_of_points):
dis_array[ind_n] = np.linalg.norm(points[ind_c] - points[ind_n])
dis_set = set(dis_array)
if len(dis_set) != len(dis_array):
for item in dis_set:
validKeys = np.in1d(dis_array, item)
validKeys[ind_c] = False
slice_length = len(dis_array[validKeys])
if slice_length > 1:
count += slice_length * (slice_length - 1)
return count
但是,这段代码遭受了Time Limit Exceeded问题的困扰,我不知道该如何处理。
这一段代码还能改进吗?我应该尝试其他策略吗?