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?

Code

Note: 注意要加上 x1.x != x2.x, 否则总是0

SELECT MIN(ABS(x1.x - x2.x)) AS shortest FROM point as x1 JOIN point as x2 WHERE x1.x != x2.x

[LeetCode] 613. Shortest Distance in a Line_Easy tag: SQL的更多相关文章

  1. [LeetCode] 821. Shortest Distance to a Character_Easy tag: BFS

    Given a string S and a character C, return an array of integers representing the shortest distance f ...

  2. 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. Writ ...

  3. [LeetCode] 317. Shortest Distance from All Buildings 建筑物的最短距离

    You want to build a house on an empty land which reaches all buildings in the shortest amount of dis ...

  4. LeetCode 821 Shortest Distance to a Character 解题报告

    题目要求 Given a string S and a character C, return an array of integers representing the shortest dista ...

  5. [LeetCode] 849. Maximize Distance to Closest Person_Easy tag: BFS

    In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is emp ...

  6. LeetCode 317. Shortest Distance from All Buildings

    原题链接在这里:https://leetcode.com/problems/shortest-distance-from-all-buildings/ 题目: You want to build a ...

  7. [LeetCode] 596. Classes More Than 5 Students_Easy tag:SQL

    There is a table courses with columns: student and class Please list out all classes which have more ...

  8. LeetCode 245. Shortest Word Distance III (最短单词距离之三) $

    This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...

  9. LeetCode 243. Shortest Word Distance (最短单词距离)$

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

随机推荐

  1. M - Pots

    You are given two pots, having the volume of A and B liters respectively. The following operations c ...

  2. AngularJS基础01 从HelloWorld说起

    作者:arccosxy  转载请注明出处:http://www.cnblogs.com/arccosxy/ 准备工作 首先,创建一个名为index.html的HTML文件,代码如下: <!DOC ...

  3. LogisticRegression 和 LogisticRegressionCV

    在scikit-learn中,与逻辑回归有关的主要是这3个类.LogisticRegression, LogisticRegressionCV 和logistic_regression_path.其中 ...

  4. layer弹窗插件自带自写经验

    复制可下载源代码和自写教程    链接: https://pan.baidu.com/s/1mhHa5Os 密码: j5su

  5. ThinkPHP框架 【 AJAX方法返回 】 例子:简单添加一条数据 和 查询一个表里的数据

    注:thinkphp使用ajax和之前使用ajax的方法一样,不同点在于之前的ajax中的url指向了一个页面,而thinkphp里面的url需要指向一个操作方法. 在模块控制器Controller文 ...

  6. 新复制行绑定JQuery.autocomplete事件

    在工作中经常需要生成动态增加行的表格,此次需要对表格中的文本框绑定autocomplete功能 操作流程: 表格初始时只有一行,当页面加载时执行先执行一次增加行功能,将行增加到10行 在$(funct ...

  7. base标签浏览器兼容问题

    <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" ...

  8. WebSphere Application Server V8.5.5.0

    Downloadable files Abstract IBM WebSphere Application Server Version 8.5.5 Refresh Pack for all plat ...

  9. SQL中的between and

    "between xx and  xx "在SQL中的用法 这个大家都很熟悉,但是当问到是否包含两端的值时,就有点儿不确定了.在W3School网站上,有这样的一段话: 参考:ht ...

  10. 依赖: nginx-common (= 1.14.0-0ubuntu1) 但是它将不会被安装

    .apt --fix-broken install .sudo apt-get remove nginx nginx-common # 卸载删除除了配置文件以外的所有文件. .sudo apt-get ...