Skip to content

Commit 971a536

Browse files
committed
[Function add]
1. Add leetcode solutions.
1 parent ff6164d commit 971a536

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,12 @@
313313

314314
[175. Combine Two Tables](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/175.%20Combine%20Two%20Tables.md)
315315

316+
[176. Second Highest Salary](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/176.%20Second%20Highest%20Salary.md)
317+
318+
[177. Nth Highest Salary](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/177.%20Nth%20Highest%20Salary.md)
319+
320+
[178. Rank Scores](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/178.%20Rank%20Scores.md)
321+
316322
## Algorithm(4th_Edition)
317323
Reading notes of book Algorithm(4th Algorithm),ISBN: 9787115293800.
318324
All java realization codes are placed in different packages.

leetcode/176. Second Highest Salary.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,18 @@ where
5252
not in(
5353
select max(Salary) from Employee
5454
)
55+
```
56+
57+
### 二刷
58+
1. 通过子查询实现筛选出第二大的数。
59+
2. 通过IFNULL判断,如果不存在则填写NULL。
60+
3. 使用DISTINCT去重。
61+
62+
```SQL
63+
# Write your MySQL query statement below
64+
SELECT IFNULL(
65+
(SELECT distinct Salary
66+
FROM Employee
67+
ORDER BY Salary desc limit 1,1)
68+
, null) as SecondHighestSalary ;
5569
```

leetcode/177. Nth Highest Salary.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,21 @@ set M = N - 1;
4242
M, 1
4343
);
4444
END
45+
```
46+
47+
### 二刷
48+
1. offset关键字后的数据不能进行数学操作。这才是我们需要在外部对N - 1进行赋值的原因。
49+
```SQL
50+
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
51+
BEGIN
52+
DECLARE M int;
53+
SET M = N - 1;
54+
RETURN (
55+
# Write your MySQL query statement below.
56+
SELECT IFNULL(
57+
(SELECT DISTINCT Salary
58+
FROM Employee
59+
ORDER BY Salary DESC LIMIT 1 OFFSET M), NULL) as Salary
60+
);
61+
END
4562
```

leetcode/178. Rank Scores.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
## 178. Rank Scores
2+
3+
### Question
4+
Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ranking. Note that after a tie, the next ranking number should be the next consecutive integer value. In other words, there should be no "holes" between ranks.
5+
6+
```
7+
+----+-------+
8+
| Id | Score |
9+
+----+-------+
10+
| 1 | 3.50 |
11+
| 2 | 3.65 |
12+
| 3 | 4.00 |
13+
| 4 | 3.85 |
14+
| 5 | 4.00 |
15+
| 6 | 3.65 |
16+
+----+-------+
17+
```
18+
19+
For example, given the above Scores table, your query should generate the following report (order by highest score):
20+
```
21+
+-------+------+
22+
| Score | Rank |
23+
+-------+------+
24+
| 4.00 | 1 |
25+
| 4.00 | 1 |
26+
| 3.85 | 2 |
27+
| 3.65 | 3 |
28+
| 3.65 | 3 |
29+
| 3.50 | 4 |
30+
+-------+------+
31+
```
32+
33+
### 二刷:
34+
1. 这道题的核心在于自己创建新的变量。
35+
2. 这道题的还牵扯到SQL的优先级问题,我们单独拎出来@pre <> (@pre := Score)进行分析。
36+
3. 实际上这行的执行顺序是从左往右,并没有将括号内的优先拉出来运算。
37+
```SQL
38+
# Write your MySQL query statement below
39+
SELECT s.Score as Score,
40+
(@i:=@i+ (@pre <> (@pre := Score))) as Rank
41+
FROM Scores s, (select @i:=0, @pre:=-1) AS init
42+
ORDER BY s.Score DESC;
43+
```
44+
45+
### 引用
46+
1. [MySQL中变量的用法——LeetCode 178. Rank Scores](http://www.cnblogs.com/rever/p/7149995.html)

0 commit comments

Comments
 (0)