美文网首页
leetcode-medium部分

leetcode-medium部分

作者: 鲸鱼酱375 | 来源:发表于2019-06-12 22:44 被阅读0次

177.Nth Highest Salary

image.png
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
DECLARE M INT;
SET M=N-1;
  RETURN (
      # Write your MySQL query statement below.
      SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT M, 1
  );
END

178.Rank Scores

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.


image.png
SELECT
  Score,
  (SELECT count(*) FROM (SELECT distinct Score s FROM Scores) tmp WHERE s >= Score) Rank
FROM Scores
ORDER BY Score desc;

在学了window function之后,这个应该还可以用window function解决

select score, dense_rank() over (order by score desc) as rank
from scores

但是不知道为什么leetcode的mysql不能通过,只有ms sql可以用

180.Consecutive Numbers

Write a SQL query to find all numbers that appear at least three times consecutively.


image.png
Select DISTINCT l1.Num as ConsecutiveNums from Logs l1, Logs l2, Logs l3 
where l1.Id=l2.Id-1 and l2.Id=l3.Id-1 
and l1.Num=l2.Num and l2.Num=l3.Num;

184.Department Highest Salary

image.png
windows function 版本
(Select e.name as employee, e.salary as salary,d.name as department ,
dense_rank() over(partition by departmentID order by e.salary desc) as rank
from employee as e
inner join department as d on e.departmentid = d.id) a
where rank = 1;
SELECT d.Name AS Department, e1.Name AS Employee, e1.Salary FROM Employee e1
JOIN Department d ON e1.DepartmentId = d.Id WHERE Salary IN 
(SELECT MAX(Salary) FROM Employee e2 WHERE e1.DepartmentId = e2.DepartmentId);

626.Exchange Seats

Mary is a teacher in a middle school and she has a table seat storing students' names and their corresponding seat ids.
The column id is continuous increment.
Mary wants to change seats for the adjacent students.
Can you write a SQL query to output the result for Mary?


image.png
select
if(id < (select count(*) from seat), if(id mod 2=0, id-1, id+1), if(id mod 2=0, id-1, id)) as id, student
from seat
order by id asc;

相关文章

  • leetcode-medium部分

    177.Nth Highest Salary 178.Rank Scores Write a SQL query ...

  • Leetcode-Medium 11. Container Wi

    题目描述 给一组非负整数数组,每个整数代表容器壁的高度,然后求最大的容量 思路 假设有一个数组a1,a2,a3.....

  • Leetcode-Medium 96.Unique Binary

    题目描述 给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种? 示例: 思路 动态规划 假设n...

  • Leetcode-Medium 739. Daily Tempe

    题目描述 根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多久温度才会升高超过该日的天数。如...

  • Leetcode-Medium 5. Longest Palin

    题目描述 给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 长度最长为1000。 Example 1...

  • Leetcode-Medium 98. Validate Bin

    题目描述 判定一棵树是否满足二叉搜索树的性质。二叉查找树(Binary Search Tree),(又:二叉搜索树...

  • Leetcode-Medium 6. ZigZag Conver

    题目描述 字符串“PAYPALISHIRING”以Z字形图案写在给定数量的行上,如下所示:(您可能希望以固定字体显...

  • Leetcode-Medium 152. Maximum Pro

    题目描述 给定一个整数数组nums(有正有负),求最大子数组乘积 思路 求最大子数组乘积问题是求最大子数组之和演变...

  • Leetcode-Medium 416. Partition E

    题目描述 给定仅包含正整数的非空数组,查找是否可以将数组划分为两个子集,使得两个子集中的元素总和相等。例子1: 例...

  • Leetcode-Medium 2. Add Two Numbe

    题目描述 给定两个非空链表,表示两个非负整数。数字以相反的顺序存储,每个节点包含一个数字。将两个数字相加并将其作为...

网友评论

      本文标题:leetcode-medium部分

      本文链接:https://www.haomeiwen.com/subject/fazrfctx.html