当前位置:网站首页>Lintcode logo queries the two nearest saplings
Lintcode logo queries the two nearest saplings
2022-07-06 14:23:00 【Lan Zhou Qianfan】
The requirement of the question is the distance between the nearest two trees in the query table . And rename the result (shortest_distance)
Answer 1 :( Adopt aggregate function and self connection )
SELECT MIN(ABS(a.distance - b.distance)) AS shortest_distance
FROM sapling_distances AS a
INNER JOIN sapling_distances b
ON a.id != b.id;
This is a very simple solution , Fields are not complicated .
Attention is paid to small details . Distance , The preconditions for finding the absolute value and the added minimum value are adopted . Then there is self join query . The condition of self connection query is the combination of both id inequality . If they are the same, then the query distance is meaningless . If id The same is comparing itself . What is the analogy between oneself and oneself ?
!= You can also use <> Instead of . It means the same thing
Explanation 2 :( Adopt nested query thinking )
SELECT MIN(distance_diff) AS shortest_distance FROM (
SELECT abs(b.distance - a.distance) AS distance_diff FROM sapling_distances a, sapling_distances b
WHERE a.id <> b.id
) cc
HAVING shortest_distance is not null;
It adopts the thinking of nested query , It's also very easy to understand . The outer layer minimizes the required result , The inner layer calculates the absolute value of the query distance . The processing of the table in memory is to name the table twice , Make it a different watch , Then attach conditions . The last thing to note is cc This is the name of the sub query table , If not , Will report a mistake , This is the grammatical requirement .
Answer 3 :( Another mode of thinking , It's nothing special )
select min(a.distance - b.distance) shortest_distance
from sapling_distances a
join sapling_distances b on a.distance > b.distance
having shortest_distance is not null;
One limitation a.distance>b.distance This replaces abs() Aggregate functions , In this way, you can also get normal results .
Solution 4 :( Superfluous solution , Simple problems complicate )
select min(s2.distance-s1.distance) shortest_distance from
(select distance,@rownum1:=@rownum1+1 r1
from sapling_distances,(select @rownum1:=0) ra
order by distance) s1,
(select distance,@rownum2:=@rownum2+1 r2
from sapling_distances,(select @rownum2:=0) rb
order by distance) s2
where s1.r1 = s2.r2-1 having shortest_distance is not null;
Don't explain , Because I won't . Solution from netizens . I really hate this solution . Simple problems complicate , Don't do it . ok ! I haven't used this method yet , When you learn it, you can add .
边栏推荐
- JDBC事务、批处理以及连接池(超详细)
- 《统计学》第八版贾俊平第十章方差分析知识点总结及课后习题答案
- How to turn wechat applet into uniapp
- [MySQL table structure and integrity constraint modification (Alter)]
- 《统计学》第八版贾俊平第十三章时间序列分析和预测知识点总结及课后习题答案
- msf生成payload大全
- AQS details
- 【MySQL-表结构与完整性约束的修改(ALTER)】
- Interpretation of iterator related "itertools" module usage
- 7-4 hash table search (PTA program design)
猜你喜欢
DVWA (5th week)
xray與burp聯動 挖掘
JDBC事务、批处理以及连接池(超详细)
Wu Enda's latest interview! Data centric reasons
Attach the simplified sample database to the SQLSERVER database instance
Callback function ----------- callback
《统计学》第八版贾俊平第十二章多元线性回归知识点总结及课后习题答案
Network technology related topics
Windows platform mongodb database installation
HackMyvm靶机系列(6)-videoclub
随机推荐
WEB漏洞-文件操作之文件包含漏洞
7-7 7003 combination lock (PTA program design)
记一次edu,SQL注入实战
Renforcer les dossiers de base de l'apprentissage
Intranet information collection of Intranet penetration (5)
Wu Enda's latest interview! Data centric reasons
HackMyvm靶机系列(2)-warrior
《统计学》第八版贾俊平第十一章一元线性回归知识点总结及课后习题答案
强化學習基礎記錄
Ucos-iii learning records (11) - task management
Hackmyvm target series (4) -vulny
Record once, modify password logic vulnerability actual combat
HackMyvm靶机系列(3)-visions
7-1 output all primes between 2 and n (PTA programming)
[paper reproduction] cyclegan (based on pytorch framework) {unfinished}
Force deduction 152 question multiplier maximum subarray
《统计学》第八版贾俊平第十二章多元线性回归知识点总结及课后习题答案
[data processing of numpy and pytoch]
Applet Web Capture -fiddler
7-9 make house number 3.0 (PTA program design)