算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !
今天和大家聊的问题叫做 平面上的最近距离,我们先来看题面:
https://leetcode.cn/problems/shortest-distance-in-a-plane/
解题
会计算点点之间的距离便可
筛选条间注意要去掉本身与本身求距离,也能够使用(p1.x,p1.y) != (p2.x,p2.y)
# Write your MySQL query statement below
select round(min(sqrt(power(p1.x-p2.x, 2)+power(p1.y-p2.y, 2))), 2) shortest
from point_2d p1, point_2d p2
where p1.x < p2.x or (p1.x = p2.x and p1.y != p2.y)
上期推文: