Leetcode 1779. Find Nearest Point That Has the Same X or Y Coordinate

浅浅的花香味﹌ 2022-09-04 05:51 73阅读 0赞

文章作者:Tyan
博客:noahsnail.com | CSDN | 简书

1. Description

Find Nearest Point That Has the Same X or Y Coordinate

2. Solution

**解析:**Version 1,碰到横纵坐标相等的点计算曼哈顿距离,并与最短距离比较,如果更短,则更新最短距离的点的索引以及最短距离。

  • Version 1

    class Solution:

    1. def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:
    2. index = -1
    3. minimum = float('inf')
    4. for i, (x1, y1) in enumerate(points):
    5. if x == x1:
    6. distance = abs(y - y1)
    7. if distance < minimum:
    8. minimum = distance
    9. index = i
    10. elif y == y1:
    11. distance = abs(x - x1)
    12. if distance < minimum:
    13. minimum = distance
    14. index = i
    15. return index

Reference

  1. https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/

发表评论

表情:
评论列表 (有 0 条评论,73人围观)

还没有评论,来说两句吧...

相关阅读