题目地址
这是一道计算两个点角度的题
首先介绍一下atan2函数
知道了这些知识之后这道题就不难了,我们计算每个点到location的角度,排序之后定义两个变量i,j;找到符合i+angle<j
的最后一个点即可,由于i在增大,所以j也无需重置。最后需要注意以下double类型的精度问题。
还有一种是和location重合的点先找出来,因为重合的点在atan2函数中是没有定义的
class Solution:
def visiblePoints(self, points: List[List[int]], angle: int, location: List[int]) -> int:
ls=[]
cnt =0
for i in points:
x=(i[0]-location[0])
y=i[1]-location[1]
if x==0 and y==0:
cnt+=1
else:
ls.append(math.atan2(y, x))
len_ls=len(ls)
for i in range(len_ls):
ls.append(ls[i] + math.acos(-1.0) * 2)
j=0
len_ls =len(ls)
ls.sort()
ans =0
for i in range(len_ls):
while j<len_ls and ls[j]-ls[i]-angle/180* math.acos(-1.0)<1e-8:
j+=1
ans=max(ans,j-i+cnt)
return ans