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?
+---------+---------+
| id | student |
+---------+---------+
| 1 | Abbot |
| 2 | Doris |
| 3 | Emerson |
| 4 | Green |
| 5 | Jeames |
+---------+---------+
For the sample input, the output is:
+---------+---------+
| id | student |
+---------+---------+
| 1 | Doris |
| 2 | Abbot |
| 3 | Green |
| 4 | Emerson |
| 5 | Jeames |
+---------+---------+
Note:
If the number of students is odd, there is no need to change the last one's seat.

解题思路:这个题目难度不大,第一眼看起来也许觉得有些繁琐,但是如果抽丝剥茧,将解题过程分解成以下几步,就会豁然开朗。

1.把所有奇数行的名字换成相邻的偶数行的名字。

select a.id,b.student from seat a left join seat b on a.id & 1  and a.id + 1 = b.id;

2.把所有偶数行的名字换成相邻的奇数行的名字。

select a.id,b.student from seat a left join seat b on a.id & 1 = 0  and a.id  - 1 = b.id;

3.合并1和2的结果

 select a.id,b.student  from seat a left join seat b
on (case when a.id & 1 then a.id + 1 = b.id else a.id -1 = b.id end);

4.如果总行数为奇数,最后一行的名字不用替换。修正SQL如下:

select a.id,case when b.student is null then a.student else b.student end  as student from seat a left join seat b
on (case when a.id & 1 then a.id + 1 = b.id else a.id -1 = b.id end);

【leetcode】Exchange Seats的更多相关文章

  1. 【LeetCode】849. Maximize Distance to Closest Person 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  3. 【Leetcode】Pascal's Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  4. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  5. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  6. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  7. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  8. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

  9. 【leetcode】657. Robot Return to Origin

    Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...

随机推荐

  1. myeclipse_2017_CI_8安装与破解

    一.下载myeclipse_2017_CI_8安装包与破解文件 二.安装myeclipse_2017_CI_8,安装完成后不要运行MyEclipse,将 "launch MyEclipse ...

  2. 关机命令 shutdown

    参考资料:[http://jingyan.baidu.com/article/49ad8bce705f3f5834d8faec.html]

  3. spring cloud依赖服务调用优化

    1.请求缓存 优点: 注解方式实现: 设置缓存key: 如果可以确认,对要缓存的数据的操作,主要是写操作都只在feign调用中完成且读多写少,则可以使用此方式:如果在其他地方还有对数据的写操作,则可能 ...

  4. docker搭建环境的时候常用的命令有哪些

    1.docker搭建环境的时候常用的命令有哪些 docker如果要删除镜像,现在停止container docker ps 查询正在运行的镜像docker stop +containerid停止后再删 ...

  5. CSP 之dvwa

    csp的本质是白名单,明确告诉浏览器哪些外部资源可以使用   请求头:[][x] Content-Security-Policy: script-src 'self'; object-src 'non ...

  6. 【神经网络与深度学习】用训练好的caffemodel来进行分类

    现在我正在利用imagenet进行finetune训练,待训练好模型,下一步就是利用模型进行分类.故转载一些较有效的相关博客. 博客来源:http://www.cnblogs.com/denny402 ...

  7. 【VS开发】【智能语音处理】语音信号处理之(四)梅尔频率倒谱系数(MFCC)

    语音信号处理之(四)梅尔频率倒谱系数(MFCC) zouxy09@qq.com http://blog.csdn.net/zouxy09 这学期有<语音信号处理>这门课,快考试了,所以也要 ...

  8. Maven使用基础

    (转)https://my.oschina.net/xiaomaoandhong/blog/104045 基于 约定优于配置(Convention Over Configuration)的原则,无特殊 ...

  9. for循环练习题:拆解字符并输入下标

    test = input('请输入:') for item in range(0,len(test)): print(item,test[item])

  10. ucloud启用redis

    可以设置密码