【leetcode】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?
+---------+---------+
| 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的更多相关文章
- 【LeetCode】849. Maximize Distance to Closest Person 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【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 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
随机推荐
- Java基础之Volatile原理
原文链接: http://www.aoaoyi.com/archives/956.html 计算机在执行程序时,每条指令都是在CPU中执行的,而执行指令过程中,势必涉及到数据 的读取和写入.由于程序运 ...
- ugui多层解决方案
创建不同的相机图层以渲染事物(最佳实践解决方案) 链接:https://answers.unity.com/questions/1169898/hi-guys-i-need-some-help-wit ...
- P4995 跳跳!
喵喵喵好久没做过贪心的题目了,刷一下免得忘了嘤嘤嘤 题面 虽然是黄题,但是我承认并不是很难,so看代码吧还是.. #include<set> #include<map> #in ...
- HCL 试验1
PC端配置:配置ip地址 交换机配置:①创建VLAN system-view vlan 10 vlan 20 ②配置PC端接口 interface gi 1/0/1 port link-type ac ...
- yum安装nginx(Centos)
测试人员需要了解Nginx?nginx的别名有很多:中间件,HTTP服务器,代理服务器等,这些名字都是作用的一个体现.在实际项目中,前后端分离,负载均衡等也是通过Nginx实现的,知己知彼,百战百胜. ...
- MySQL-线上数据迁移实战记录
1. 迁移背景和限制条件 随着功能的迭代或者数据表中数据量的增加,将现有数据进行迁移已是工作中经常遇到的事情.通常我们在平时迁移数据数据的时候,只需要用mysqldump.mysqlimport指令就 ...
- IA-32 Assembly Language Reference Manual
Load Full Pointer (lds,les, lfs, lgs, and lss) lds{wl} mem[32|48], reg[16|32]les{wl} mem[32|48], reg ...
- 【三】Django模版的使用
作为Web框架,Django提供了模板,可以很便利的动态生成HTML 模版系统致力于表达外观,而不是程序逻辑 模板的设计实现了业务逻辑(view)与显示内容(template)的分离,一个视图可以使用 ...
- qt使用QWT注意事项
当继承某个QWT类时,有是使用O_OBJECT弘会出现问题 切记在工程文件里别忘了添加这一句 DEFINES+=QWT_DLL
- django Paginator 让分页变得完美
参考大佬地址:https://www.zmrenwu.com/courses/django-blog-tutorial/materials/21/ 类视图 from django.contrib.au ...