zoukankan      html  css  js  c++  java
  • 626. 换座位

    地址:https://leetcode-cn.com/problems/exchange-seats/

    ## 小美是一所中学的信息科技老师,她有一张 seat 座位表,平时用来储存学生名字和与他们相对应的座位 id。
       
       其中纵列的 id 是连续递增的
       
       小美想改变相邻俩学生的座位。
       
       你能不能帮她写一个 SQL query 来输出小美想要的结果呢?
       
    
         SQL架构
         示例:
         
         +---------+---------+
         |    id   | student |
         +---------+---------+
         |    1    | Abbot   |
         |    2    | Doris   |
         |    3    | Emerson |
         |    4    | Green   |
         |    5    | Jeames  |
         +---------+---------+
         假如数据输入的是上表,则输出结果如下:
         
         +---------+---------+
         |    id   | student |
         +---------+---------+
         |    1    | Doris   |
         |    2    | Abbot   |
         |    3    | Green   |
         |    4    | Emerson |
         |    5    | Jeames  |
         +---------+---------+
         注意:
         
         如果学生人数是奇数,则不需要改变最后一个同学的座位。
    `解题思路`
    
    1 使用case then
        
        mod(id, 2) = 1  确定是否为奇数
    
        `select (
             case
                 when mod(id,2) != 0 and counts != id then id+1
                 when mod(id,2) != 0 and counts = id then id
                 else id-1
             end) as id,student
         from seat,
              (select count(*) as counts from seat) as seat_counts
         order by id asc;
    `
         
  • 相关阅读:
    动词的形态及变化(转)
    数论基础
    P1505 [国家集训队]旅游
    贪心常见题
    主席树
    卡常火车头
    AC自动机
    左偏树
    位运算
    Linux下Vim常用操作
  • 原文地址:https://www.cnblogs.com/8013-cmf/p/13164508.html
Copyright © 2011-2022 走看看