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. C#打印标签

    一个复杂的标签包括一个复杂的表格样式和二维码.条形码等内容.所以如果直接绘制的方式将会非常的麻烦,所以采用使用的方案是使用模板的方式:1.使用Excel创建出想要的模板的样式.2.对模板中的动态内容进 ...

  2. python中的过滤fliter

    movie_people = ['sb_alex', 'sb_wupeiqi', 'hello'] def filter_test(array): ret = [] for p in array: i ...

  3. pytorch的torch.utils.data.DataLoader认识

    PyTorch中数据读取的一个重要接口是torch.utils.data.DataLoader,该接口定义在dataloader.py脚本中,只要是用PyTorch来训练模型基本都会用到该接口, 该接 ...

  4. 记录jq控制select 选中状态

    $("#categoryId option[value='"+ data.category_id +"']").attr("selected" ...

  5. 关于ionic2 更新到ionic3 后组件不能用的解决方案

    错误代码就不贴出来了,直接上代码吧! 首先在xx.module.ts添加 1.import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; 2.添加 ...

  6. xcode工程编译错误:The maximum number of apps for free development profiles has been reached.

    真机调试免费App ID出现的问题The maximum number of apps for free development profiles has been reached.免费应用程序调试最 ...

  7. [development][dpdk] dpdk与CONFIG_RTE_MAX_MEMSEG有关的错误

    dpdk程序无法启动, 错误如下: EAL: Requesting pages of size 2MB EAL: Requesting pages of size 2MB EAL: Can only ...

  8. [adminstrative][CentOS] CentOS 7 常用操作笔记

    CentOS从6换到7, 还没有系统的学习.虽然主要用的是systemd,但还是有一下特有的区别,会逐步整理如下: 官网文档索引:https://access.redhat.com/documenta ...

  9. AndroidStudio_TextView

    写APP的工具为:AndroidStudio 主要在两种文件里编辑代码:.xml和.java(.xml里主要是调整布局相当于APP的前端 .java主要是写Java程序相当于APP的后端) 代码的调试 ...

  10. 转:Redis 3.2.1集群搭建

    Redis 3.2.1集群搭建   一.概述 Redis3.0版本之后支持Cluster. 1.1.redis cluster的现状 目前redis支持的cluster特性: 1):节点自动发现 2) ...