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.

  1. | x |
  2. |-----|
  3. | -1 |
  4. | 0 |
  5. | 2 |

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

  1. | shortest|
  2. |---------|
  3. | 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

  1. 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. 在PowerShell中使用Vim

    1.需要去Vim官网下载并安装一个可运行于Win8系统的执行文件(ftp://ftp.vim.org/pub/vim/pc/gvim74.exe). 2.设置PowerShell环境,使能“allow ...

  2. nodejs抓取页面内容,并分析有无某些内容的js文件

    nodejs获取网页内容绑定data事件,获取到的数据会分几次相应,如果想全局内容匹配,需要等待请求结束,在end结束事件里把累积起来的全局数据进行操作! 举个例子,比如要在页面中找有没有www.ba ...

  3. Error:..\FreeRTOS\portable\RVDS\ARM_CM3\port.c,378 Error:..\FreeRTOS\portable\RVDS\ARM_CM3\port.c,378 Error:..\FreeRTOS\portable\RVDS\ARM_CM3\port.c,378 Error:..\FreeRTOS\tasks.c,2806

    Error:..\FreeRTOS\portable\RVDS\ARM_CM3\port.c,378Error:..\FreeRTOS\portable\RVDS\ARM_CM3\port.c,378 ...

  4. Vue 超快速学习

    Vue 超快速学习 基础知识: 1.vue的生命周期: beforeCreate/created. beforeMount/mounted. beforeUpdate/updated. beforeD ...

  5. toggle显示与隐藏切换

    jQuery中显示与隐藏切换toggle方法 show与hide是一对互斥的方法.需要对元素进行显示隐藏的互斥切换,通常情况是需要先判断元素的display状态,然后调用其对应的处理方法.比如显示的元 ...

  6. Page12:镇定条件、镇定与极点配置的关系,解耦控制的概念等[Linear System Theory]

    内容包含镇定条件.镇定与极点配置的关系,解耦控制的概念.形式.分类以及各种解耦方法特点,系统能否解耦判断.

  7. [dpdk] dpdk --lcores参数

    dpdk程序的命令行参数 --lcores可以设置lcore到CPU processer的多对多映射关系. 这样既可以提供CPU processor的伸缩扩展,同时也保证了EAL thread的运行环 ...

  8. [knowledge][lisp] lisp与AI

    https://blog.youxu.info/2009/08/31/lisp-and-ai-1/ https://blog.youxu.info/2010/02/10/lisp-and-ai-2/

  9. 关于.htaccess的设置

    RewriteEngine On #设置是否开始rewrite RewriteBase / #设置开始匹配的目录,比如web程序放在/var/www/html/test下,则这个值要设置为" ...

  10. pyhton 爬虫爬去吾爱精品软件的信息并写入excel

    2018的最后一天了,感觉今年有得有失,这里就不再浪费时间了,愿2019万事如意 之前的爬虫信息下载后只写入txt文档,想到了以后工作加入有特殊需求,趁放假有时间将这写数据写入excel表格 以吾爱精 ...