zoukankan      html  css  js  c++  java
  • leetcode刷题笔记 一百七十八题 && 一百八十题

    leetcode刷题笔记 一百七十八题 && 一百八十题

    源地址:

    178. 分数排名

    180. 连续出现的数字

    178问题描述:

    编写一个 SQL 查询来实现分数排名。

    如果两个分数相同,则两个分数排名(Rank)相同。请注意,平分后的下一个名次应该是下一个连续的整数值。换句话说,名次之间不应该有“间隔”。

    +----+-------+
    | Id | Score |
    +----+-------+
    | 1 | 3.50 |
    | 2 | 3.65 |
    | 3 | 4.00 |
    | 4 | 3.85 |
    | 5 | 4.00 |
    | 6 | 3.65 |
    +----+-------+
    例如,根据上述给定的 Scores 表,你的查询应该返回(按分数从高到低排列):

    +-------+------+
    | Score | Rank |
    +-------+------+
    | 4.00 | 1 |
    | 4.00 | 1 |
    | 3.85 | 2 |
    | 3.65 | 3 |
    | 3.65 | 3 |
    | 3.50 | 4 |
    +-------+------+
    重要提示:对于 MySQL 解决方案,如果要转义用作列名的保留字,可以在关键字之前和之后使用撇号。例如 Rank

    # Write your MySQL query statement below
    SELECT Scores.Score, DENSE_RANK() OVER(ORDER BY Score DESC) AS 'Rank' FROM Scores;
    
    #rank函数 有并列名次的行,会占用下一名次的位置
    #dense_rank函数 有并列名次的行 不占用下一名词的位置
    #row_number函数 不考虑并列名次问题
    

    180问题描述:

    编写一个 SQL 查询,查找所有至少连续出现三次的数字。

    +----+-----+
    | Id | Num |
    +----+-----+
    | 1 | 1 |
    | 2 | 1 |
    | 3 | 1 |
    | 4 | 2 |
    | 5 | 1 |
    | 6 | 2 |
    | 7 | 2 |
    +----+-----+
    例如,给定上面的 Logs 表, 1 是唯一连续出现至少三次的数字。

    +-----------------+
    | ConsecutiveNums |
    +-----------------+
    | 1 |
    +-----------------+

    # Write your MySQL query statement below
    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 l1.Num = l3.Num;
    
  • 相关阅读:
    HackerRank
    HackerRank
    LeetCode "Kth Smallest Element in a BST"
    HackerRank
    HackerRank
    LeetCode "Roman to Integer"
    LeetCode "Integer to Roman"
    LeetCode "Majority Element II"
    HackerRank
    HackerRank
  • 原文地址:https://www.cnblogs.com/ganshuoos/p/13651029.html
Copyright © 2011-2022 走看看