Leetcode 1779. Find Nearest Point That Has the Same X or Y Coordinate
Posted SnailTyan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 1779. Find Nearest Point That Has the Same X or Y Coordinate相关的知识,希望对你有一定的参考价值。
文章作者:Tyan
博客:noahsnail.com | CSDN | 简书
1. Description
2. Solution
**解析:**Version 1,碰到横纵坐标相等的点计算曼哈顿距离,并与最短距离比较,如果更短,则更新最短距离的点的索引以及最短距离。
- Version 1
class Solution:
def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:
index = -1
minimum = float('inf')
for i, (x1, y1) in enumerate(points):
if x == x1:
distance = abs(y - y1)
if distance < minimum:
minimum = distance
index = i
elif y == y1:
distance = abs(x - x1)
if distance < minimum:
minimum = distance
index = i
return index
Reference
以上是关于Leetcode 1779. Find Nearest Point That Has the Same X or Y Coordinate的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode 1779. 找到最近的有相同 X 或 Y 坐标的点