LeetCode 613. Shortest Distance in a Line

简介: LeetCode 613. Shortest Distance in a Line

Table point holds the x coordinate of some points on x-axis in a plane, which are all integers.

Write a query to find the shortest distance between two points in these points.

| x   |
|-----|
| -1  |
| 0   |
| 2   |

The shortest distance is '1' obviously, which is from point '-1' to '0'. So the output is as below:

| shortest|
|---------|
| 1       |

Note: Every point is unique, which means there is no duplicates in table point.

Follow-up: What if all these points have an id and are arranged from the left most to the right most of x axis?

题目描述:求两点间的最短距离。

题目分析:利用 sql 语句查询两点间最短距离,将 x1 表和 x2 表连接起来,去除两个表的重复项即可。

MySQL 语句如下:

SELECT 
  MIN(ABS(x1.x - x2.x)) AS shortest 
FROM 
  point AS x1 
JOIN 
  point AS x2 
WHERE 
  x1.x != x2.x
目录
相关文章
|
存储
LeetCode 329. Longest Increasing Path in a Matrix
给定一个整数矩阵,找出最长递增路径的长度。 对于每个单元格,你可以往上,下,左,右四个方向移动。 你不能在对角线方向上移动或移动到边界外(即不允许环绕)。
70 0
LeetCode 329. Longest Increasing Path in a Matrix
LeetCode 149. Max Points on a Line
给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上。
101 0
LeetCode 149. Max Points on a Line
|
算法 索引
LeetCode 214. Shortest Palindrome
给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串。找到并返回可以用这种方式转换的最短回文串。
90 0
LeetCode 214. Shortest Palindrome
Leetcode-Easy 461.Hamming Distance
Leetcode-Easy 461.Hamming Distance
109 0
Leetcode-Easy 461.Hamming Distance
【1046】Shortest Distance (20 分)
【1046】Shortest Distance (20 分) 【1046】Shortest Distance (20 分)
91 0
1046. Shortest Distance (20)
The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits.
961 0
[LeetCode] Longest Line of Consecutive One in Matrix 矩阵中最长的连续1
Given a 01 matrix M, find the longest line of consecutive one in the matrix. The line could be horizontal, vertical, diagonal or anti-diagonal.
1557 0