HackerRank-SQL

MySQL 函数

🟢 easy 🟡 medium 🔴 hard

1. Type of Triangle 🟢

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
-- select 
-- case
-- when A=B and B=C then "Equilateral"
-- when A=B or B=C or A=C then "Isosceles"
-- --(1 1 0 is not a triangle, next line should come befor this line)
-- when A+B<=C or A+C<=B or B+C<=A then "Not A Triangle"
-- else "Scalene"
-- end
-- from TRIANGLES

select
case
when A+B<=C or A+C<=B or B+C<=A then "Not A Triangle"
when A=B and B=C then "Equilateral"
when A=B or B=C or A=C then "Isosceles"
else "Scalene"
end
from TRIANGLES

-- A+B>C -> A+B+C>2*C greatest(A,B,C):select max
select
case
when greatest(A,B,C)*2>=(A+B+C) then "Not A Triangle"
when A=B and B=C then "Equilateral"
when A=B or B=C or A=C then "Isosceles"
else "Scalene"
end
from TRIANGLES

2. Binary Tree Nodes 🟡

1
2
3
4
5
6
7
8
9
10
11
12
13
select N,
case
when P is null then 'Root'
when N in(select distinct P from BST) then 'Inner'
else 'Leaf'
end
from BST
order by N;

--
select N, if(P is null, 'Root', if(N in(select distinct P from BST), 'Inner', 'Leaf'))
from BST
order by N;

To be updated…


HackerRank-SQL
http://example.com/2021/10/13/hackerrankSQL/
作者
K
发布于
2021年10月13日
许可协议