[LeetCode] 613. Shortest Distance in a Line_Easy tag: SQL
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的更多相关文章
- [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 ...
- 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 ...
- [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 ...
- LeetCode 821 Shortest Distance to a Character 解题报告
题目要求 Given a string S and a character C, return an array of integers representing the shortest dista ...
- [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 ...
- LeetCode 317. Shortest Distance from All Buildings
原题链接在这里:https://leetcode.com/problems/shortest-distance-from-all-buildings/ 题目: You want to build a ...
- [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 ...
- 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 ...
- LeetCode 243. Shortest Word Distance (最短单词距离)$
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
随机推荐
- python使用selenium安装chromedriver的问题
环境 win64位,python3.6, 问题与解决 说来也巧,今天无意中解决了两个多月前的问题,即用selenium调用chrome浏览器报错的问题:起因是在知乎中看到了一篇12306抢票的文章,用 ...
- python3安装PIL
原创 2017-09-29 16:15:27 系统环境: 64位win10系统,同时安装python2.7与python3.6两个版本 安装: PIL是Python平台事实上的图像处理标准库,支 ...
- db2 MON_GET_PKG_CACHE_STMT 表函数 抓取分析SQL
MON_GET_PKG_CACHE_STMT 表函数 还可以使用 MON_GET_PKG_CACHE_STMT 表函数来查询当前 PACKAGE CACHE 中 SQL 语句(包括动态 SQL 和静态 ...
- [No000011B]为什么有些程序员悄无声息渡过35岁中年危机?
今天分享是一些已经渡过中年危机的前辈们,看看从他们身上,是如何优雅的过渡的.如果想一直在程序这条路上走下去,建议读完.文章略长. 人物一:陈睿,前百度研发经理,携程定制旅游CTO 从程序员到架构师到管 ...
- CSS 小技巧
CSS 小技巧 一.边框内圆角 我们在设计例如按钮等控件的时候,会遇到这样的设计:只有内侧有圆角,而边框或者描边的四个角还是保持直角的形状,用以下代码可以轻松的实现. #wrapper { width ...
- 2012年蓝桥杯省赛A组c++第1题(xy迭代增殖)
/* 微生物增殖 题目: 假设有两种微生物 X 和 Y X出生后每隔3分钟分裂一次(数目加倍),Y出生后每隔2分钟分裂一次(数目加倍). 一个新出生的X,半分钟之后吃掉1个Y,并且,从此开始,每隔1分 ...
- linux学习:【第2篇】常用命令
狂神声明 : 文章均为自己的学习笔记 , 转载一定注明出处 ; 编辑不易 , 防君子不防小人~共勉 ! linux学习:[第2篇]常用命令 基本命令 //打开终端: CentOS:在任何地方,右键-- ...
- [未解决:快速滑动collectionveiw请求数据崩溃]:unable to allocate 6553600 bytes for bitmap data
崩溃:unable to allocate 6553600 bytes for bitmap data
- ORACLE监听配置及测试实验(2)
实验四 在tnsname.ora里添加默认监听代号 [oracle@oracle01 admin]$ vi tnsnames.ora 添加一行 PORT1528=(ADDRESS = (PROTOCO ...
- LeetCode 557 Reverse Words in a String III 解题报告
题目要求 Given a string, you need to reverse the order of characters in each word within a sentence whil ...